title: Why is buffering in C++ important?
body: <p>I tried to print <code>Hello World</code> 200,000 times and it took me forever, so I have to stop. But right after I add a <code>char</code> array to act as a buffer, it took less than 10 seconds. Why?</p>
  
  <p>Before adding a buffer:</p>
  
  <pre><code>#include &lt;iostream&gt; 
  using namespace std;
  
  int main() {
          int count = 0;
          std::ios_base::sync_with_stdio(false);
          for(int i = 1; i &lt; 200000; i++)
          {       
                  cout &lt;&lt; "Hello world!\n";
                  count++;
          }
                  cout&lt;&lt;"Count:%d\n"&lt;&lt;count;
  return 0;
  }
  </code></pre>
  
  <p>And this is after adding a buffer:</p>
  
  <pre><code>#include &lt;iostream&gt; 
  using namespace std;
  
  int main() {
          int count = 0;
          std::ios_base::sync_with_stdio(false);
          char buffer[1024];
          cout.rdbuf()-&gt;pubsetbuf(buffer, 1024);
          for(int i = 1; i &lt; 200000; i++)
          {       
                  cout &lt;&lt; "Hello world!\n";
                  count++;
          }
                  cout&lt;&lt;"Count:%d\n"&lt;&lt;count;
  return 0;
  }
  </code></pre>
  
  <p>This makes me think about Java. What's the advantages of a using BufferReader to read in file?</p>