performances question

Started by
2 comments, last by masterbubu 12 years, 6 months ago
Hi,

I'm wondering, what would be faster.

having an IF condition on a loop which decides to what function to call VS pre-bind a function pointer according to the same condition, and then call the function pointer on the loop.


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



Advertisement
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.
Invariant hoisting. It's about as specific of an answer as one can give.
Hi,

I'm coding on VS 2010.

tnx for the help :)

This topic is closed to new replies.

Advertisement