It looks like a conveyor rack
int iFreq = getFreq(i); double dFreq = iFreq; if (iFreq != 0) {
Allows you to convert a double value in parallel with other code since dFreq is not used immediately. it gives the compiler something to do this between saving iFreq and using it, so this conversion is most likely โFreeโ.
But
int iFreq = getFreq(i); if (iFreq != 0) { // do some stuff with iFreq... double dFreq = iFreq; // do some stuff with dFreq... }
There may be a click on the storage / control stop after conversion to double, since you immediately start using a double value.
Modern processors can perform several operations per cycle, but only when things are independent. Two consecutive instructions that reference the same register often lead to a halt. The actual conversion to double can take 3 clock cycles, but everything except the first hours can be performed in parallel with other work, if you do not refer to the conversion result for an instruction or two.
C ++ compilers do pretty well with reordering instructions to take advantage of this, it looks like your change has distorted some good optimization.
Another (less likely) possibility is that when the conversion to float was before the branch, the compiler was able to completely remove the branch. Unprofitable code is often a major gain in performance in modern processors.
It would be interesting to see what instructions the compiler actually emitted for these two cases.
John knoeller
source share