3

I have a little program :

public class Main {
  public static void main(String[] args) {
    for(int p = 0;p<10;p++) {
      long start  = System.currentTimeMillis();
      int j = Integer.MAX_VALUE;
      int d = 2;
      int c = 0;
      for(int i =Integer.MAX_VALUE; i>0;i--) {
        if(i==Integer.MAX_VALUE)System.out.println(j);
        c=j/d;
      }
      System.out.println(System.currentTimeMillis()-start);
    }   
  }
}

Here is the result I get:

$ java Main
2147483647
62              // Almost instantaneous
2147483647
1437            // Normal speed
2147483647
1455            // Same
...

I made sure it's not a simple bug by disabling JIT, and in that case all iterations are the same speed:

$ java -Djava.compiler=NONE Main
2147483647
26879
2147483647
26484

2 questions

  1. Why does first itteration through external loop take so much less time then other 9 ?(every time i run this)
  2. How come, the fastest(slight difference, but still) division from those 3: c>>1, c/2, c/d is the last one ?
that other guy
  • 116,971
  • 11
  • 170
  • 194
kamnielos
  • 31
  • 1

0 Answers0