title: printf vs cout performance
body: <pre><code>void perfprint(unsigned int count)
  {
      char a[100] = "fosjkdfjlsjdflw0304802";
      for(unsigned int i = 0;i&lt;count;++i)
      {
          printf("%s", a);
      }
  }
  
  void perfcout(unsigned int count)
  {
      char a[100] = "fosjkdfjlsjdflw0304802";
      for(unsigned int i = 0;i&lt;count;++i)
      {
          cout &lt;&lt; a;
      }
  }
  </code></pre>
  
  <p>Environment : C++, VS 2010, Windows 7, 32-bit, Core-i7, 4GB, 3.40 GHz</p>
  
  <p>I tested both the functions with <code>count = 10000</code> for <code>5</code> times each.
  Measured the performance using <code>QueryPerformanceCounter</code>.</p>
  
  <p><code>perfprint</code> > <code>~850</code> milliseconds (Avg of 5 runs)</p>
  
  <p><code>perfcout</code>  > <code>~9000</code> milliseconds (Avg of 5 runs)</p>
  
  <p>Does this mean <code>printf</code> is <code>~10x</code> faster than <code>cout</code>?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>With /Ox, /Ot, No debug information in Release build</p>
  
  <p>and with <code>std::ios_base::sync_with_stdio(false);</code> in <code>perfcout</code> method,
  result is same for <code>cout</code> i.e. <code>~9000 millisecs</code></p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>To conclude, <code>cout</code> is faster than <code>printf</code>. The reason of the observations above were due to console output. When redirecting output to file, things turned on its head! </p>