title: Benchmark C++ vs Java, Unrealistic results
body: <p>I did a simple test, I know C++ is faster but the results of my test is unrealistic.</p>
  
  <p>C++ code is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;windows.h&gt;
  
  unsigned long long s(unsigned long long n)
  {
      unsigned long long s = 0;
  
      for (unsigned long long i = 0; i &lt; n; i++)
          s += i;
  
      return s;
  }
  
  int main()
  {
      LARGE_INTEGER freq, start, end;
      QueryPerformanceFrequency(&amp;freq);
      QueryPerformanceCounter(&amp;start);
  
      printf("%llu\n", s(1000000000));
  
      QueryPerformanceCounter(&amp;end);
      double d = (double) (end.QuadPart - start.QuadPart) / freq.QuadPart * 1000.0;
  
      printf("Delta: %f\n", d);
  
      return 0;
  }
  </code></pre>
  
  <p>Java code is:</p>
  
  <pre><code>public class JavaApplication5 {
  
      public static long s(long n) {
          long s = 0;
  
          for (long i = 0; i &lt; n; i++) {
              s += i;
          }
  
          return s;
      }
  
      public static void main(String[] args) {
  
          long start = System.nanoTime();
  
          System.out.println(s(1000000000));
  
          long end = System.nanoTime();
  
          System.out.println((end - start)/1000000);
      }
  }
  </code></pre>
  
  <p><em><sup>C++ compiler: gcc 4.4.0 and Java: jdk 1.6.0</sup></em></p>
  
  <blockquote>
    <p><code>Java: 2795 ms</code></p>
    
    <p><code>C++ : 0.013517 ms</code></p>
  </blockquote>
  
  <p>It says C++ is 206777 times faster than Java! No way! What is wrong in my test?</p>