In general, does spliting a large function in smallers one increase its speed?
In general, does spliting a large function in smallers one increase its speed?
I don't know if splitting it up would increase speed, but I do know that making a function do too many things defeats the whole purpose of using a function in the first place. A rule of thumb is this: A function should ideally do one thing, and do it well.
In general, all other factors being equal -- no. There is a very small slice of time needed, for any call to another function, to set up the stack.
With multi-layered cache's, and modern compilers however, you can be certain that all other factors are NOT equal.
If you want to optimize your program:
1) Find the least big O algorithm that clearly is accurate. Avoid optimizing yet.
2) Use a profiler on typical input, and see where any bottlenecks are, in the run time.
3) Optimize your bottlenecked sections of code.
4) And re-test with the profiler.
Test first, test last, and avoid making the code obscenely obscure or "cute". In general, it's a bad idea to generalize - you want tests, not hand-waving and smoke blowing.
> In general, does spliting a large function in smallers one increase its speed?
It increases the speed you can write, test, debug and maintain it.
If you can't see the whole function on screen at the same time, then you should consider splitting it up.
There are obvious exceptions, say a large switch/case in a top-level message decoder splitting the work into specific functions.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Splitting a function up arbitrarily doesn't help anything.
Splitting a function up into specific sub-tasks helps productivity, but doesn't really affect execution speed.
Finding an operation, even a small one, within the large function, that is performed more than once and factoring that out into a separate function, can improve execution speed as well as productivity.
All "in general" of course.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"