title: C++ (Intel) vs Java (Hotspot) vs C# benchmark questions (code and results included)
body: <p>I have been comparing the raw CPU performance speed between the three main languages (code and results are below). I am very curious as to how the main languages compare for raw computational power. I had a theory that Java and C# could possibly rival C++ when there was no memory overhead involved. </p>
  
  <p>My questions:</p>
  
  <p>1) Edited (C++ timings now more realistic)</p>
  
  <p>2) Am I right in thinking the JVM took ages on the first iteration, but for the second it had finished analysing and therefore optimised? How did Hotspot know to finish optimising after the first iteration of my outside loop and not halfway through?</p>
  
  <p>3) Why does C# not perform like Java and heavily optimise at the start? What is different about C# with regards to Java? Why is the C# slower- is it simply due to less optimization?</p>
  
  <p>4) Is there any specific reason why the oscillation between 2246 and 2262 milliseconds for the C# test timings, could this be two different times because the CPU has two cores?</p>
  
  <p><strong>EDIT: Updating code to show stopwatch usage in C# code.</strong></p>
  
  <p><strong>EDIT: Correct C++ timing code and results</strong></p>
  
  <p>The setup:</p>
  
  <ul>
  <li><p>C++: VS2010 and Intel Compiler (built in release mode, Optimization:
  O2, Enable intrinsic functions: yes, favor size nor speed: neither,
  omit frame pointers: No, enable fiber-safe optimizations: no, whole
  program optimization: yes)</p></li>
  <li><p>Java: Eclipse, Hotspot 64 bit compiler version 17, Java 1.6</p></li>
  <li><p>C#: VS2010 and .net 4.0 (built in release mode)</p></li>
  <li><p>CPU: Intel E6600 (2.4GHz) running at 2.7GHz, bus speed 300MHz, 8GB memory, DRAM Freq: 375MHz </p></li>
  <li>Win 7 (64 bit)</li>
  </ul>
  
  <p><strong>C++ code:</strong></p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;mmsystem.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;fstream&gt; 
  
  using namespace std;
  
  
  double PCFreq = 0.0;
  __int64 CounterStart = 0;
  
  void StartCounter()
  {
      LARGE_INTEGER li;
      if(!QueryPerformanceFrequency(&amp;li))
          cout &lt;&lt; "QueryPerformanceFrequency failed!\n";
  
      PCFreq = li.QuadPart;
  
      QueryPerformanceCounter(&amp;li);
      CounterStart = li.QuadPart;
  }
  double GetCounter()
  {
      LARGE_INTEGER li;
      QueryPerformanceCounter(&amp;li);
      return double(li.QuadPart-CounterStart)/PCFreq;
  }
  
  static long counter = 0;
  
  int _tmain(int argc, _TCHAR* argv[])
  {
  
      for (int m = 0; m &lt; 10; m++)
      {
          StartCounter();
          counter = 0;
  
          for (int j = 0; j &lt; 3; j++)
          {
              //Just to test timing is working correctly
              //int* p = new int;
  
              for (long i = 0; i &lt; 200000000; i++)
              {
                  counter++;
              }
          }
  
          cout &lt;&lt; GetCounter()*1000000 &lt;&lt; " microseconds" &lt;&lt; endl;
      }
  
  
      int p = 0;
      cin &gt;&gt; p;
      return 0;
  }
  </code></pre>
  
  <p><strong>C++ results:</strong></p>
  
  <blockquote>
    <p>7.19 microseconds</p>
    
    <p>1.89</p>
    
    <p>2.27</p>
    
    <p>1.51</p>
    
    <p>4.92</p>
    
    <p>10.22</p>
    
    <p>10.22</p>
    
    <p>9.84</p>
    
    <p>9.84</p>
    
    <p>10.6</p>
  </blockquote>
  
  <p><strong>Java code:</strong></p>
  
  <pre><code>public class main {
  
      static long counter = 0;
  
      public static void main(String[] args) {
  
          for(int m=0; m&lt;10; m++){
              long start = System.nanoTime();
              counter = 0;
  
              for(int j=0;j&lt;3; j++){
                  for(long i=0; i&lt;200000000; i++){
                      counter++;
                  }
              }
  
              System.out.println(((System.nanoTime()-start)/1000000) + " ms");
          }
      }
  }
  </code></pre>
  
  <p><strong>Java results:</strong></p>
  
  <pre><code>5703 milliseconds
  471 ms
  468 ms
  467 ms
  469 ms
  467 ms
  467 ms
  467 ms
  469 ms
  464 ms
  </code></pre>
  
  <p><strong>C# code:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Diagnostics
  
  namespace t1
  {
      class Program
      {
          static long counter = 0;
  
          static void Main(string[] args)
          {
              for (int m = 0; m &lt; 10; m++)
              {
                  Stopwatch s = new Stopwatch();
                  s.Start();
                  counter = 0;
  
                  for (int j = 0; j &lt; 3; j++)
                  {
  
                      for (long i = 0; i &lt; 200000000; i++)
                      {
                          counter++;
                      }
  
                  }
                  s.Stop();
                  Console.WriteLine(s.Elapsed.TotalMilliseconds + " ms");
              }
  
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  
  <p><strong>C# results:</strong></p>
  
  <blockquote>
    <p>2277 milliseconds</p>
    
    <p>2246 ms</p>
    
    <p>2262 ms</p>
    
    <p>2246 ms</p>
    
    <p>2262 ms</p>
    
    <p>2246 ms</p>
    
    <p>2262 ms</p>
    
    <p>2246 ms</p>
    
    <p>2262 ms</p>
    
    <p>2262 ms</p>
  </blockquote>