Since I am no working with C++ and no longer use Java as my mainly programming language, I was wondering whether some of the past experiments would yield different results. In this blog post I will repeat the exercise of comparing floats and doubles but now for C++. Given that Java and C++ do handle types differently it is possible that the results may vary, however both languages implement floating points numbers in the same way, according to the IEEE standard.
Although the purpose of this test is not to compare Java with C++, I do repeat the exact same tests as for Java. These tests consists of:
- Add 0.001 on each iteration.
- Subtract 0.001 on each iteration (starting with 200.000).
- Add a value which gets halved upon each iteration (starting with 1000).
- Add a variable value between 0.001 and 0.099.
- Switch between adding and subtracting an increasing value (iteration * 0.1).
All of the tests have been executed with the GCC compiler optimizer set to level 3. As a result, I had to print the results of the calculation to avoid the compiler from completely removing all calculations. So the compiler was smart enough to discover that the calculations had no effect and completely removed the code. The printing of the result was done after the time recording and thus is not included in the recorded time.
The results are very similar to those of Java. First of all we have to discuss the results of the computations again, just as with Java the results of scenario’s 1, 2 and 4 are wrong when performed with floats. This is of course caused by the same implementation of floating numbers. We do however also see that as with Java, the difference between floating point and doubles is negligible.
What does pop out is that scenario 4 takes a lot longer than all cases. This scenario however uses random numbers and I point to a quick and dirty usage of random numbers as the cause why this scenario takes so long. The difference here between Java and C++ is that there is no way to ask for a random double in C++, instead I had to do all the scaling and conversions myself. This implementation is likely to be sub-optimal and I have done no effort to see whether I could improve it, since it is the comparison between floating point and doubles that mattered to me.
So in general we can draw the same conclusion as we did with Java, there isn’t really any point to use floats except for memory usage. But whenever choosing for floats, make sure that the result of the operation is still reliable and the rounding errors don’t dominate your result.
Just for the sake of doing it, we could make a quick comparison between Java and C++, the table below shows the results for the doubles of each scenario.
The general idea that people have, being that C++ is more performant than Java seems to hold , except for my bad implementation of random which is something interesting to be investigated later. Even though C++ can sometimes be twice as fast, the nominal difference is not that big. Going from 350ms to 120 is not something you will easily notice in your application, especially not because this improvement is achieved when doing 100 million operations!