|
-
July 14th, 2009, 09:14 AM
#1
Floating point operations is slower when small values are used?
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?
[CODE]
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");
}
[CODE]
Result:
Number A) Time elapsed: 3546 ms
Number B) Time elapsed: 110 ms
Thanks,
Diego
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|