Hi,
Today I tried to solve 286C - Main Sequence, which required to read & print 106 ints.
There are already many blog posts that compares performance of cin, cout (with ios::sync_with_stdio(false)
of course) vs printf, scanf. And the test results shown that for ints, cin and cout are equally fast (and sometimes even faster) than scanf, printf. But my submissions do not agree:
- scanf + printf: 9881086 --> AC 966ms
- scanf + cout: 9881146 --> AC 1434ms
- cin + printf: 9881138 --> AC 1684ms
- cin + cout: 9882075 --> TLE
Note: I used cout << endl;
in my submissions, but it prints endl only twice, so that should not be a problem here.
As you can see, except from input/output, my code is exactly the same. Am I doing anything wrong here?
UPD: I found another problem that cin/cout failed:
- My submission with cin/cout: 9919391
- A previously accepted code that use cin/cout but now got TLE: 9919511. This problem has warning "Package for this problem was not updated...", but the code doesn't use anything fancy, and also the printf/scanf version passed system test in 436ms (time limit is 1000ms). So I'm guessing that it is indeed because of cin/cout.
So, most likely, ios::sync_with_stdio(false);
does not work well with the GNU C++ 4.9.2 which is used in Codeforces.
Try using cin.tie(0).
there is cin.tie(0) in last submission
Oh, sorry, I didn't notice it.
On some submissions, I had
cin.tie(0)
, but it did not help. Usually that only results in 100ms difference.Maybe mingw was updated recently.
It makes sense to find one of posts about it and recheck same problem, they checked
Maybe that only happens on Codeforces, on my PC cin/cout are significantly faster.
Also SPOJ, Differences are too big between cin/cout and scanf/printf in SPOJ.
endl flushes the stream:
printf("%d\n",i) + fflush(0)
7.477scout<<i<<endl
8.696sprintf("%d\n",i)
0.515scout<<i<<"\n"
1.654sthere's just 2 endl's, anyway substitution them to "\n" doesn't help
BTW, It's seems that sync_with_stdio(false) doesn't affect cout (here, at CF) at all(I've tested your second submission with this line commented and got ~ same time.