I have the following simple program that multiplies two different floating point numbers many times. As you can see, one of the numbers is very small. When I calculate the time of executing both multiplications, I was surprised that the little number takes much longer than the other one. It seems that working with small doubles is slower... Does anyone know what is happening?
public static void main(String[] args) throws Exception
{
long iterations = 10000000;
double result;
double number = 0.1D;
double numberA = Double.MIN_VALUE;
double numberB = 0.0008D;
long startTime, endTime,elapsedTime;
//Multiply numberA
startTime = System.currentTimeMillis();
for(int i=0; i < iterations; i++)
result = number * numberA;
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
System.out.println("Number A) Time elapsed: " + elapsedTime + " ms");
//Multiply numberB
startTime = System.currentTimeMillis();
for(int i=0; i < iterations; i++)
result = number * numberB;
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
System.out.println("Number B) Time elapsed: " + elapsedTime + " ms");
}
Result:
Number A) Time elapsed: 3546 ms Number B) Time elapsed: 110 ms
Platform Windows, Java 1.6.0_07
Thanks, Diego