Updated: Add note for Cygwin environment.
Updated: Add result on FreeBSD
In programming competition such as Codeforces, reading a large number of inputs is somtimes required, for example, 1500*1500 integers in Codeforces Beta Round #43 Problem E. I used iostream(cin) in the competition and got TLE. After the competition, I saw some comments that cin caused TLE. After I replaced cin with scanf, I got AC. So, it is said "For large input, use cstdio instead of iostream." However I believe C++ users want to use iostream. Can't we avoid the guideline?
I found one promising way. Unfortunately, it is applicable not to VC but to gcc. Call std::ios_base::sync_with_stdio(false) at the beginning of your code. Here is the graph to show performance comparison among iostream with the call, without the call and cstdio, in my local environments. [Updated: NOTE that File I/O on Cygwin is generally very slow. Therefore, the effect is overestimated in graph on Cygwin environment.]
X-axis shows the number of inputs and Y-axis shows seconds to read inputs. The scale of Y-axis is different between graphs.
The summary of my local environment is as follows:
- CPU: Core2Duo T7600 2.3Ghz
- RAM: 4GB
- OS: Windows XP SP3
- Compiler: gcc 4.3.4 on Cygwin 1.7.7
- CPU: PentiumM 1.3GHz
- RAM: 1GB
- OS: FreeBSD 8.1-STABLE
- Compiler: gcc 4.2.1
Target source code is here (at codepad.org).
Though I don't know the precise effect in the judge environment, I can get AC with iostream, at least, for Codeforces Beta Round #43 Problem E. NOTE that the effect of the call depends on implementation of C++ standard library. For example, there is no significant difference for VC++.
Calling sync_with_stdio(false) may improve performance. What is the actual effect of the call?
The standard (ISO/IEC 14882:2003) says:
220.127.116.11 ios_base static members
bool sync_with_stdio(bool sync = true);
Returns: true if the standard iostream objects (27.3) are synchronized and otherwise returns false.
The first time it is called, the function returns true.
Effects: If any input or output operation has occurred using the standard streams prior to the call, the effect is implementation-defined. Otherwise, called with a false argument, it allows the standard streams to operate independently of the standard C streams.
This means that synchronization with standard C stream is required without the call. In libstdc++ (C++ standard library used by g++), this flag switches iostream object's underlying buffer between stdio_filebuf and stdio_sync_filebuf. stdio_sync_filebuf has little buffering by itself and delegate almost all operation to cstdio. See also libstdc++ manual page.
For programming competition, there seems to be little use of mixture of cstdio and iostream. Thus, I recommend that you include the call in your code template. :p
I did not recognize the post. One of the reason is that I can not read Russian. :p
I found sync_with_stdio(false) in googling "iostream performance" or so. There are some talks about iostream poor performance. However, it is distributed and does not have clear conclusion. So, I think making this post would be useful for others.
I had a strange experience, once iostream with sync...(false) was faster than cstdio but once it's been exactly reverce.the first one was on this problem and the second was sgu's 302.and here are the codes:first, second.I made a little change in the getting input & printing output on the codes to test the differences.but the main codes are the same and have the same results(u can test it :D).
once I heard that using getchar() is the fastest way at all. could you please add this test to your results?
Fastest way is to read all the data into a large buffer at once and then parse it yourself.
could you plz explain it more,and could you plz post a code.i haven't heard anything about it.
For example, reading an array of N non-negative numbers separated by space:
Solution with scanf/printf: 5666032 (156 ms)
Solution with buffered i/o: 5666061 (62 ms)
In its pure form such i/o faster than scanf/printf almost 10 times.
thank you.it helped me a lot.
You can see some tests here: http://codeforces.com/blog/entry/5217
time limit = 1s;
here using ios_base::sync_with_stdio(0); cin.tie(NULL);
give TLE but using scanf and printf instead gives AC. running time = 0.561s.
cin, cout may fail even after using ios_base....
Nevermind, I did it myself and you are absolutely correct.
I think its because the problem is not about integers, but long integers. In this case iostream probably is as bad as it is for double (twice as slow).
iostream TLE http://codeforces.com/gym/100741/submission/13717970
stdio AC http://codeforces.com/gym/100741/submission/13718007
The limit is 10^9 so integers suffice, you don't need long integers. Also I had one problem with long longs where I got accepted with iostream but TLE with stdio.
Why so many downvotes? Yes, the comment I replied to is from 3 years ago but the article itself is from 8 years ago and the other late comments don't have a negative rating. I've just read this article and thought that villasv would still like to hear my feedback. Also, I thought only paras and villasv would get notified about my comment. Could it be that because it's my first comment here codeforces notifies everyone about it? If there is something wrong in what I said, please correct me.