First of all, I think your int
benchmark is giving a specious answer. The number simply fails the common sense test!!
Incrementing from 1 to 1,000,000,000 should perform 1 billion increments, 1 billion stores, 1 billion tests, 1 billion branches. All of that is supposedly happening in ~0.004 seconds. That implies a 4 billion instructions in 0.004 seconds, or 1 trillion instructions in 1 second ... or a clock rate of some multiple of 1.0E12. But the fastest intel processors around have clock rate less than 10 GHz ... or 1.0E10.
In fact, what I think has happened is that the JIT compiler has figured out that your loop is doing no useful work, and has optimized it away.
For some reason, the JIT compiler has not figured out that the version with the double
is also not doing useful work either.
But either way, 1 billion floating point operations in 3 seconds using a single processor is pretty good performance ...
My question is how to improve this really bad performance of double in java
It is not really bad performance.
It is (approximately) real performance ... and the int
performance you are seeing is bogus.
You can't improve on it.
Actually, you are kind of lucky that the double
loop terminates at all. Consider this version:
for (double i = 0; i < Integer.MAX_VALUE; i = i + 1){}
for (float i = 0; i < Integer.MAX_VALUE; i = i + 1){}
The float
version should be faster. Right?
Wrong!
The problem is that a float
only has 24 bits (7.22 decimal digits) of precision. So, somewhere between 1.0E7 and 1.0E8, you will get to a point where i + 1
evaluates to i
... due to rounding. That leads to an infinite loop.
When you think about it, it is this kind of thing that could cause the JIT compiler to not optimize away a floating point loop.