So, there's this rule to try to pull if
statements out of high repetition loops:
for( int i = 0 ; i < 10000 ; i++ )
{
if( someModeSettingOn ) doThis( data[i] ) ;
else doThat( data[i] ) ;
}
They say, it's better to break it up, to put the if statement outside:
if( someModeSettingOn )
for( int i = 0 ; i < 10000 ; i++ )
doThis( data[i] ) ;
else
for( int i = 0 ; i < 10000 ; i++ )
doThat( data[i] ) ;
(In case you're saying "Ho! Don't optimize that yourself! The compiler will do it!") Sure the optimizer might do this for you. But in Typical C++ Bullshit (which I don't agree with all his points, eg his attitude towards virtual functions) Mike Acton says "Why make the compiler guess at something you know? Pretty much best point of those stickies, for me.
So why not use a function pointer instead?
FunctionPointer *fp ;
if( someModeSettingOn ) fp = func1 ;
else fp = func2 ;
for( int i = 0 ; i < 10000 ; i++ )
{
fp( data[i] ) ;
}
Is there some kind of hidden overhead to function pointers? Is it is efficient as calling a straight function?
if
statement. – Specialist