title: Adding numbers using Java Long wrapper versus primitive longs
body: <p>I am running this code and getting unexpected results. I expect that the loop which adds the primitives would perform much faster, but the results do not agree.</p>
  
  <pre><code>import java.util.*;
  
  public class Main {
      public static void main(String[] args) {
          StringBuilder output = new StringBuilder();
          long start = System.currentTimeMillis();
          long limit = 1000000000; //10^9
          long value = 0;
          for(long i = 0; i &lt; limit; ++i){}
          long i;
          output.append("Base time\n");
          output.append(System.currentTimeMillis() - start + "ms\n");
          start = System.currentTimeMillis();
          for(long j = 0; j &lt; limit; ++j) {
              value = value + j;
          }
          output.append("Using longs\n");
          output.append(System.currentTimeMillis() - start + "ms\n");
          start = System.currentTimeMillis();
          value = 0;
          for(long k = 0; k &lt; limit; ++k) {
              value = value + (new Long(k));
          }
          output.append("Using Longs\n");
          output.append(System.currentTimeMillis() - start + "ms\n");
          System.out.print(output);
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <p><strong>Base time
  359ms
  Using longs
  1842ms
  Using Longs
  614ms</strong></p>
  
  <p>I have tried running each individual test in it's own java program, but the results are the same. What could cause this?</p>
  
  <p>Small detail: running java 1.6</p>
  
  <p>Edit:
  I asked 2 other people to try out this code, one gets the same exact strange results that I get. The other gets results that actually make sense! I asked the guy who got normal results to give us his class binary. We run it and we STILL get the strange results. The problem is not at compile time (I think). I'm running 1.6.0_31, the guy who gets normal results is on 1.6.0_16, the guy who gets strange results like I do is on 1.7.0_04.</p>
  
  <p>Edit: Get same results with a Thread.sleep(5000) at the start of program. Also get the same results with a while loop around the whole program (to see if the times would converge to normal times after java was fully started up)</p>