title: c# to c++ dictionary to unordered_map results
body: <p>I've done a few years of c# now, and I'm trying to learn some new stuff. So I decided to have a look at c++, to get to know programming in a different way.</p>
  
  <p>I've been doing loads of reading, but I just started writing some code today.</p>
  
  <p>On my Windows 7/64 bit machine, running VS2010, I created two projects:
  1) A c# project that lets me write things the way I'm used to.
  2) A c++ "makefile" project that let's me play around, trying to implement the same thing. From what I understand, this ISN'T a .NET project.</p>
  
  <p>I got to trying to populate a dictionary with 10K values. For some reason, the c++ is orders of magnitude slower.</p>
  
  <p><strong>Here's the c# below.</strong> Note I put in a function after the time measurement to ensure it wasn't "optimized" away by the compiler:</p>
  
  <pre><code>var freq = System.Diagnostics.Stopwatch.Frequency;
  
  int i;
  Dictionary&lt;int, int&gt; dict = new Dictionary&lt;int, int&gt;();
  var clock = System.Diagnostics.Stopwatch.StartNew();
  
  for (i = 0; i &lt; 10000; i++)
       dict[i] = i;
  clock.Stop();
  
  Console.WriteLine(clock.ElapsedTicks / (decimal)freq * 1000M);
  Console.WriteLine(dict.Average(x=&gt;x.Value));
  Console.ReadKey(); //Don't want results to vanish off screen
  </code></pre>
  
  <p><strong>Here's the c++</strong>, not much thought has gone into it (trying to learn, right?)
      int input;</p>
  
  <pre><code>LARGE_INTEGER frequency;        // ticks per second
  LARGE_INTEGER t1, t2;           // ticks
  double elapsedTime;
  
  // get ticks per second
  QueryPerformanceFrequency(&amp;frequency);
  
  int i;
  boost::unordered_map&lt;int, int&gt; dict;
  // start timer
  QueryPerformanceCounter(&amp;t1);
  
  for (i=0;i&lt;10000;i++)
      dict[i]=i;
  
  // stop timer
  QueryPerformanceCounter(&amp;t2);
  
  // compute and print the elapsed time in millisec
  elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
  cout &lt;&lt; elapsedTime &lt;&lt; " ms insert time\n";
  int input;
  cin &gt;&gt; input; //don't want console to disappear
  </code></pre>
  
  <p>Now, some caveats. <a href="https://stackoverflow.com/questions/686483/c-vs-c-big-performance-difference/686617#686617">I managed to find this related SO question.</a> One of the guys wrote a long answer mentioning WOW64 skewing the results. I've set the project to release and gone through the "properties" tab of the c++ project, enabling everything that sounded like it would make it fast. Changed the platform to x64, though I'm not sure whether that addresses his wow64 issue. I'm not that experienced with the compiler options, perhaps you guys have more of a clue?</p>
  
  <p><strong>Oh, and the results: c#:0.32ms c++: 8.26ms.</strong> This is a bit strange. Have I misinterpreted something about what .Quad means? I copied the c++ timer code from someplace on the web, going through all the boost installation and include/libfile rigmarole. Or perhaps I am actually using different instruments unwittingly? Or there's some critical compile option that I haven't used? Or maybe the c# code is optimized because the average is a constant?</p>
  
  <p><strong>Here's the c++ command line,</strong> from the Property page->C/C++->Command Line:
  /I"C:\Users\Carlos\Desktop\boost_1_47_0" /Zi /nologo /W3 /WX- /MP /Ox /Oi /Ot /GL /D "_MBCS" /Gm- /EHsc /GS- /Gy- /arch:SSE2 /fp:fast /Zc:wchar_t /Zc:forScope /Fp"x64\Release\MakeTest.pch" /Fa"x64\Release\" /Fo"x64\Release\" /Fd"x64\Release\vc100.pdb" /Gd /errorReport:queue </p>
  
  <p>Any help would be appreciated, thanks.</p>