C++ double comparison performance

I ran the code beloew but with different version of the "if" statement. Do either boolean, int or double comparisons. Astonishingly, they all give the same order of timings! I would have assumed that boolean would be MUCH fatser than doubles.

I should probably tell you that your compiler probably turned this into:

for (i=0;i<4000000000;i++)
cc = 2.0+i;

or even just did the math before hand(depending on options, and compiler).

It might even turn declare int as a double to compare it, if the int does nothing else.
Depending on the compiler(and options) it's going to use the best instructions for the selected(or default) target architecture, which means the tick deference might not be much.
Or it could do a million things that would effect this poor code.

As MosaicFuneral points out, the compiler can do thousands of things. BUT the best way to find out is to examing the machine code.

Then we learn the following:

The key error in the test code is that you use da/db regardless of the change to the line if (ba==bc) . If you carefully remove that, and stop the strong optimizer settings making cc = 4e7 and skipping the loop. [Well done portland!] then you end up with the the boolean taking slightly shorter than the double on my linux amd 64bit machine under gcc-4.3 and portland. Intel handle integers differently to amd and that is going to change the results as well. Also if you didn't compile your compiler for you exact cpu then you are also going to get rubbish results.

You mileage will differ depending on machine/compiler/OS/Load on the machine. It is normal to do tests a couple of times and THEN do the actual test.

also I dont think you intended to write da == cc in the first if statement (my compiler said statement without effect . It guess you didn't compile with warnings.

The key thing with testing for cpu time, is do it with your real-world problems, then if you make an improvement, you have made a real improvement!