3 ms·
> For example, if you have a variable 'v' and want to update it to a new value 'f', you can do 'if (v == f)' to check if the variable would change. Isn't it st
by stouset 4y ago
> For example, if you have a variable 'v' and want to update it to a new value 'f', you can do 'if (v == f)' to check if the variable would change.
Isn't it strictly faster to simply assign? No branching, no branch predictions, only one instruction in all cases.
- kstenerud 4y agoIt depends on whether you need a set operation or a test-and-set operation.
- karpierz 4y agoAssuming that all you want to do is assign, then sure. If you want to log the change or do any additional conditional logic, then that wouldn't work.
- spacechild1 4y agoA typically pattern is: float freq = getParameter(FILTER_FREQUENCY); float coeff; if (freq != mFreq) { coeff = calculateCoeffFromFrequency(freq); // expensive mFreq = freq; mCoeff = coeff; } else { coeff = mCoeff; // use cached value } // use coeff
- bee_rider 4y agoCould you do some threshold-based comparison? What if freq is changed only a little tiny bit?
- spacechild1 4y agoI can certainly imagine cases where this would make sense.