The Short Version
In the following line:
aData[i] = aData[i] + ( aOn * sin( i ) );
If aOn
is 0
or 1
, does the processor actually perform the multiplication, or does it conditionally work out the result (0
for 0
, other-value for 1
)?
The Long Version
I'm looking into algorithm performance consistency, which partly involves a look into the effect of Branch Prediction.
The hypothesis is that this code:
for ( i = 0; i < iNumSamples; i++ )
aData[i] = aData[i] + ( aOn * sin( i ) );
will provide more stable performance than this code (where branch prediction may destabilise performance):
for ( i = 0; i < iNumSamples; i++ )
{
if ( aOn )
aData[i] = aData[i] + sin( i );
}
with aOn
being either 0
or 1
, and it can toggle during the loop execution by another thread.
The actual conditional calculation (+ sin( i )
in the example above) involves more processing and the if condition must be within the loop (there are multitude of conditions, not just one like in the example above; also, changes to aOn
should have effect immediately and not per loop).
Ignoring performance consistency, the performance tradeoff between the two options is in the time it takes to execute the if
statement and that of a multiplication.
Regardless, it is easy to spot that if a processor would not perform the actual multiplication for values like 1
and 0
, the first option could be a win-win solution (no branch prediction, better performance).