Compiler Performance – When Do Function Call Costs Matter?

functionsperformance

I am a religious person and make efforts not to commit sins. That is why I tend to write small (smaller than that, to reword Robert C. Martin) functions to comply with the several commandments ordered by the Clean Code bible. But while checking some stuff, I landed on this post, below which I read this comment:

Remember that the cost of a method call can be significant, depending
on the language. There's almost always a tradeoff between writing
readable code and writing performant code.

Under what conditions is this quoted statement still valid nowadays given the rich industry of performant modern compilers?

That is my only question. And it is not about whether I should write long or small functions. I just highlight that your feedback may -or not- contribute to altering my attitude and leave me unable to resist the temptation of blasphemers.

Best Answer

It depends on your domain.

If you are writing code for low-power microcontroller, then method call cost might be significant. But if you are creating normal website or application, then method call cost will be negligible compared to rest of the code. In that case, it will always be more worth focusing on right algorithms and data structures instead of micro-optimizations like method calls.

And there is also question of compiler inlining the methods for you. Most compilers are intelligent enough to inline functions where it is possible.

And last, there is golden rule of performance : ALWAYS PROFILE FIRST. Don't write "optimized" code based on assumptions. If you are unusure, write both cases and see which is better.

Related Topic