Short version
In the next line:
aData[i] = aData[i] + ( aOn * sin( i ) );
If aOn is 0 or 1 , does the processor really perform the multiplication or does it conditionally return the result ( 0 for 0 , another value for 1 )?
Long version
I look at consistency in algorithm performance, which is partly due to the Prediction of Branches effect.
This code is supposed to be:
for ( i = 0; i < iNumSamples; i++ ) aData[i] = aData[i] + ( aOn * sin( i ) );
will provide more stable performance than this code (where branch prediction can destabilize performance):
for ( i = 0; i < iNumSamples; i++ ) { if ( aOn ) aData[i] = aData[i] + sin( i ); }
with aOn is either 0 or 1 , and it can switch during the execution of the loop by another thread.
The actual conditional calculation ( + sin( i ) in the above example) involves a lot of processing and the if condition must be inside the loop (there are many conditions, not just one, as in the above example, and changes in aOn should have an effect immediately, not per cycle).
Ignoring performance consistency, the trade-off between the two parameters is the time it takes to execute the if and the multiply operation.
Despite this, it is easy to notice that if the processor does not perform the actual multiplication for values ββsuch as 1 and 0 , the first option may be a win-win solution (no branch prediction, higher performance).
c ++ performance c algorithm processors
Izhaki
source share