Trevor Hickey
Trevor Hickey

Reputation: 37806

Increase Program Speed By Avoiding Functions? (C++)

When it comes to procedural programming, functional decomposition is ideal for maintaining complicated code. However, functions are expensive- adding to the call stack, passing parameters, storing return addresses. all of this takes extra time! When speed is crucial, how can I get the best of both worlds? I want a highly decomposed program without any necessary overhead introduced by function calls. I'm familiar with the keyword: "inline" but that seems to be only be a suggestion to the compiler, and if used incorrectly by the programmer it will yield an even slower program. I'm using g++, so will the -03 flag optimize away my functions that call functions that call functions.. I just wanted to know, if my concerns are valid and if there are any methods to combat this issue.

Upvotes: 2

Views: 900

Answers (3)

PeddleSpam
PeddleSpam

Reputation: 438

If it's a specific piece of code you're worried about you can measure the time yourself. Just run it in a loop a large number of times and get the system time before and after. Use the difference to find the average time of each call.

As always the numbers you get are subjective, since they will vary depending on your system and compiler. You can compare the times you get from different methods to see which is generally faster, such as replacing the function with a macro. My guess is however you won't notice much difference, or at the very least it will be inconsequential.

If you don't know where the slowdown is follow J.N's advice and use a code profiler and optimise where it's needed. As a rule of thumb always pass large objects to functions by reference or const reference to avoid copy times.

Upvotes: 1

Drew Chapin
Drew Chapin

Reputation: 7989

I highly doubt speed is that curcial, but my suggestion would be to use preprocessor macros.

For example

#define max(a,b) ( a > b ? a : b )

This would seem obvious to me, but I don't consider myself an expect in C++, so I may have misunderstood the question.

Upvotes: -1

J.N.
J.N.

Reputation: 8421

First, as always when dealing with performance issues, you should try and measure what are your bottlenecks with a profiler. The first thing coming out is usually not function calls and by a large margin. If you did this, then please read on.

Then, you can anticipate a bit what functions you want inlined by using the inline keyword. The compiler is usually smart enough to know what to inline and what not to inline (it can inline functions you forgot and may not inline some you mentionned if he thinks it won't help).

If (really) you still want to improve performance on function calls and want to force inlining, some compilers allow you to do so (see this question). Please consider that massive inlining may actually decrease performance: your code will use a lot of memory and you may get more cache misses on the code than before (which is not good).

Upvotes: 4

Related Questions