0

I have this code in Java, using two different types of loops.

public class Test {
    public static void main(String[] args){
        long fl = 0, wl = 0; 
        int i = 0;
        int a = 0;
        long start = 0, stop = 0;

        start = System.currentTimeMillis();
        while(i<2000000000){
             if(i%2 == 0)
                a++;
            else
                a--;
            i++;
        }
        stop = System.currentTimeMillis();
        wl = stop-start/2;
        System.out.println("\nWhile loop = "+wl);

        i = 0;
        a = 0;
        start = 0;
        stop = 0;

        start = System.currentTimeMillis();
        for(;i<2000000000;){
            if(i%2 == 0)
                a++;
            else
                a--;
            i++;
        }
        stop = System.currentTimeMillis();
        fl = stop-start/2;
        System.out.println("For loop = "+fl);

        System.out.println("Difference = "+(fl-wl));
    }
}

Now, after running the program multiple times, I have come to the conclusion that the second loop is always executing slower than the first loop. At first, I thought it had something to do with one being a for loop and the other being a while loop, but even when I reversed the order, the second loop still executes slower. Here is the output of a sample run.

While loop = 688721817947
For loop = 688721824295
Difference = 6348

Now, why is this happening.

1
  • 1
    Would you really like to try explaining a difference that is less than 0.000001%? Aug 25, 2013 at 15:21

3 Answers 3

6

You calculate your time based on

fl = stop-start/2;

due to operator precedence:

fl = stop - (start / 2)

That's not what you want I guess, as executing it 100 ms later would cause your fl variable to be 50ms "longer" ((stop + 100) - ((start + 100) / 2) = stop - (start / 2) + 50). That could be a reason why the second one is always "slower".

1
  • 688721817947 milliseconds is over 20 years, making the original output obviously wrong. Aug 25, 2013 at 15:44
1

A difference that small is really negligible, and it's difficult if not impossible to determine what caused it. The bytecode of your two loops is identical:

while-loop:

  21: goto          43
  24: iload         5
  26: iconst_2      
  27: irem          
  28: ifne          37
  31: iinc          6, 1
  34: goto          40
  37: iinc          6, -1
  40: iinc          5, 1
  43: iload         5
  45: ldc           #22                 // int 2000000000
  47: if_icmplt     24

for-loop:

 104: goto          126
 107: iload         5
 109: iconst_2      
 110: irem          
 111: ifne          120
 114: iinc          6, 1
 117: goto          123
 120: iinc          6, -1
 123: iinc          5, 1
 126: iload         5
 128: ldc           #22                 // int 2000000000
 130: if_icmplt     107
0

That difference has no sense, as 6348 / 688721824295 aprox 9-E9. That is, less than a 1-E6 %.

Anything could cause the difference, from the OS processing a thread from another process or the antivirus kicking it or a cosmic ray causing interference. It is like asking why car A makes a route in 1 hour and another makes it in 1 hour and 1 millionth of a second.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.