The overhead of calling functions is mainly:
- function call itself
- parameters that you pass
- retun value
- the number of times you need to call a function
So, for starters, ask questions:
- Can you change the algorithm to require fewer function calls?
- can you reduce the amount of data you need to transfer back and forth?
- Can you change the algorithm for batch processing calls to each function (so that you can process a group of values in one call or at least repeat the same function for a group of values so that all the code remains in the processor cache)?
Once you have a good algorithm and efficient implementation, you will have to move on to lower level optimization methods - you can use assembler to execute your own function call protocol, which requires less data that needs to be pushed onto the stack. If they are “worksheet functions” (which do not call other functions), you may not even need to use the stack, so you can avoid several overhead instructions with each call. (Some of this could be done in C by replacing the calls to gotos functions - this is very ugly, though)
Finally, you can fall into the realms of self-modifying code - build a new machine code from fragments representing functions, and then call the generated code. This can be very processor specific and complex, although it is quite low.
source share