Sign in to follow this  
masterbubu

performances question

Recommended Posts

Hi,

I'm wondering, what would be faster.

having an [b][size="3"]IF[/size][/b] condition on a loop which decides to what function to call VS [size="3"][b]pre-bind a function pointer[/b] [/size]according to the same condition, and then call the function pointer on the loop.


Note: the condition is bool flag, nothing to expensive.



Share this post


Link to post
Share on other sites
Depends. Which platform?

A branch is always more expensive than no branch, but the PC has a lot of logic to handle branching and branch prediction. On the other hand, the function pointer is effectively a virtual call, e.i. the compiler have no choice but to make a full-blown function call. So you have a function call on each iteration of the loop. With the branching version the compiler have more optimization options (it can inline the function call for example.)

The only way to answer this question is to profile the actual code (not a small contrived sample, since the size and workload in the functions matter). But I'll bet that unless your loop have a gazillion iterations it wont even show up in the profiler. Remember what they say about early optimizations. Pick the version that is clearest to understand, easiest to read, and makes most sense in the context, and then IF you later find that it is a bottleneck, THEN start looking for a better solution. Which might well be to change the algorithm instead of the loop.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this