# Floating point operations is slower when small values are used?

• 07-14-2009, 04:49 PM
Cesaro
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"); }```

Result:
Number A) Time elapsed: 3546 ms
Number B) Time elapsed: 110 ms

Thanks,
Diego
• 07-14-2009, 08:04 PM
angryboy
its a matter of precision, for example:
Code:

```public class Test {   static long iterations = 10000000;   static long startTime, endTime;   static double result, elapsedTime;     public static void main(String[] args){                    time(Double.MIN_VALUE);      time(4.90000e-324);     time(4.90000e-320);     time(4.90000e-315);     time(4.90000e-310);     time(4.90000e-309);     time(4.90000e-308);//slow     time(4.90000e-307);//fast     time(4.90000e-306);     time(4.90000e-305);     time(Double.MAX_VALUE);   }   public static void time(double d){     startTime = System.nanoTime();     for(int i=0; i < iterations; i++)       result = 0.1D * d;     endTime = System.nanoTime();     elapsedTime = (endTime - startTime)/1000000D;     System.out.printf("Number %g\tTime elapsed: %.4f ms\n", d, elapsedTime);   } }```
Code:

```Number 4.90000e-324    Time elapsed: 1388.6648 ms Number 4.90000e-324    Time elapsed: 1513.0510 ms Number 4.90000e-320    Time elapsed: 1721.3305 ms Number 4.90000e-315    Time elapsed: 1512.3794 ms Number 4.90000e-310    Time elapsed: 1644.4087 ms Number 4.90000e-309    Time elapsed: 1946.8857 ms [B]Number 4.90000e-308    Time elapsed: 1699.0190 ms Number 4.90000e-307    Time elapsed: 99.3045 ms[/B] Number 4.90000e-306    Time elapsed: 94.0902 ms Number 4.90000e-305    Time elapsed: 94.9939 ms Number 1.79769e+308    Time elapsed: 191.4065 ms```