Results 1 to 2 of 2
  1. #1
    Cesaro is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Default 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?

    Java 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

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    its a matter of precision, for example:
    Java 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);
      }
    }
    Java 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
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Replies: 2
    Last Post: 04-01-2009, 09:37 AM
  2. java floating point comparison
    By sardare in forum Advanced Java
    Replies: 6
    Last Post: 03-03-2009, 05:11 PM
  3. number of floating point
    By mohammad8065 in forum Advanced Java
    Replies: 5
    Last Post: 12-28-2008, 10:41 AM
  4. Floating point values in SWT Spinner
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:50 PM

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
  •