Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>By default, <code>cin</code> is synchronized with stdio, which causes it to avoid any input buffering. If you add this to the top of your main, you should see much better performance:</p> <pre><code>std::ios_base::sync_with_stdio(false); </code></pre> <p>Normally, when an input stream is buffered, instead of reading one character at a time, the stream will be read in larger chunks. This reduces the number of system calls, which are typically relatively expensive. However, since the <code>FILE*</code> based <code>stdio</code> and <code>iostreams</code> often have separate implementations and therefore separate buffers, this could lead to a problem if both were used together. For example:</p> <pre><code>int myvalue1; cin &gt;&gt; myvalue1; int myvalue2; scanf("%d",&amp;myvalue2); </code></pre> <p>If more input was read by <code>cin</code> than it actually needed, then the second integer value wouldn't be available for the <code>scanf</code> function, which has its own independent buffer. This would lead to unexpected results.</p> <p>To avoid this, by default, streams are synchronized with <code>stdio</code>. One common way to achieve this is to have <code>cin</code> read each character one at a time as needed using <code>stdio</code> functions. Unfortunately, this introduces a lot of overhead. For small amounts of input, this isn't a big problem, but when you are reading millions of lines, the performance penalty is significant.</p> <p>Fortunately, the library designers decided that you should also be able to disable this feature to get improved performance if you knew what you were doing, so they provided the <a href="http://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio" rel="noreferrer"><code>sync_with_stdio</code></a> method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload