title: massive performance difference for an empty loop between java 32 and 64 bit
body: <p>In my free time i recently made a framework for multi-threaded math operations, and to test it i calculated the first couble of thousand prime numbers.</p>
  
  <p>But i needed it to take more time, so i inserted this code into the prime calculation:</p>
  
  <pre><code>for (int i = 0; i &lt; 1000000; i++)
  {
      // Nothing.
  }
  </code></pre>
  
  <p>For a long time, i write and compiled the code on a 64bit machine, and tested it on a number of 32 bit machines. </p>
  
  <p>Then i ran it on a 64 bit machine, and noticed a massive performance difference. </p>
  
  <p>With the same code, a completely similar 64 machine  takes &lt;100ms to do, what a 32 machine uses ~52000ms to do (2 virtual machine on the same host). </p>
  
  <p>I've tested on Windows and Ubuntu on different computers, and using the same .class file, i still get this massive 32bit vs 64bit difference. </p>
  
  <p>Here is a quick code that you can use the replicate the performance difference. </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Collection;
  public class Test {
  public static void main(String[] args)
  {
      long start = System.currentTimeMillis();
      int j = 2;
      ArrayList&lt;Integer&gt; res = new ArrayList&lt;Integer&gt;();
      for (int k = 0; k &lt; 50000; k++)
      {
          Collection&lt;Integer&gt; partres = work(k);
          if (partres != null)
              res.addAll(work(k));
      }
      long end = System.currentTimeMillis();
      System.out.println("Done in " + (end-start) + " ms.");
  }
  public static Collection&lt;Integer&gt; work(Integer j) {
      for (int i = 0; i &lt; 1000000; i++)
      {
          // Nothing.
      }
      if (isPrime(j))
      {
          ArrayList&lt;Integer&gt; res = new ArrayList&lt;Integer&gt;();
          res.add(j);
          return res;
      }
      else
          return null;
  }
  static boolean isPrime(int n) {
      if (n == 2) return true;
      if (n%2==0) return false;
      for(int i = 3; i * i &lt;= n; i += 2) 
          if(n%i==0)
              return false;
      return true;
  }
  }
  </code></pre>
  
  <p>And <a href="http://webbies.dk/tmp/Test.class" rel="nofollow">here is the .class file i compiled it to.</a> </p>
  
  <p>Now my question. </p>
  
  <p>I know that there is a performance gain by using a 64bit machine, but that doesn't explain this massive difference. So does anybody have any idea why this is happening?</p>