title: printf more than 5 times faster than std::cout?
body: <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cstdio&gt;
  #include &lt;ctime&gt;
  
  int main(int argc, char* argv[])
  {
      std::clock_t start;
      double duration;    
  
      std::cout &lt;&lt; "Starting std::cout test." &lt;&lt; std::endl;
      start = std::clock();
  
      for (int i = 0; i &lt; 1000; i++)
      {
          std::cout &lt;&lt; "Hello, World! (" &lt;&lt; i &lt;&lt; ")" &lt;&lt; std::endl;
      }
  
      duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
  
      std::cout &lt;&lt; "Ending std::cout test." &lt;&lt; std::endl;
      std::cout &lt;&lt; "Time taken: " &lt;&lt; duration &lt;&lt; std::endl;
  
      std::system("pause");
  
      std::cout &lt;&lt; "Starting std::printf test." &lt;&lt; std::endl;
      start = std::clock();
  
      for (int i = 0; i &lt; 1000; i++)
      {
          std::printf("Hello, World! (%i)\n", i);
          std::fflush(stdout);
      }
  
      duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
  
      std::cout &lt;&lt; "Ending std::printf test." &lt;&lt; std::endl;
      std::cout &lt;&lt; "Time taken: " &lt;&lt; duration &lt;&lt; std::endl;
  
      system("pause");
  
      return 0;
  }
  </code></pre>
  
  <p>Now, here are the times for the first five runs:</p>
  
  <ul>
  <li>std::cout test: <strong>1.125</strong> s ;  printf test: <strong>0.195</strong> s</li>
  <li>std::cout test: <strong>1.154</strong> s ;  printf test: <strong>0.230</strong> s</li>
  <li>std::cout test: <strong>1.142</strong> s ;  printf test: <strong>0.216</strong> s</li>
  <li>std::cout test: <strong>1.322</strong> s ;  printf test: <strong>0.221</strong> s</li>
  <li>std::cout test: <strong>1.108</strong> s ;  printf test: <strong>0.232</strong> s</li>
  </ul>
  
  <p>As you can see, using <code>printf</code> and then <code>fflush</code>ing takes about 5 times less time than using <code>std::cout</code>. </p>
  
  <p>Although I did expect using <code>std::cout</code>'s <code>&lt;&lt;</code> operator to be perhaps a little slower (almost minimal) , I wasn't prepared for this huge difference. Am I making a fair test? If so, then what makes the first test so much slower than the second one, if they essentially do the exact same thing?</p>