q234rty's blog

By q234rty, history, 4 years ago, In English

TL;DR Scanf/printf is very slow in the GNU C++17 (64) compiler. Use cin/cout with ios_base::sync_with_stdio(0);cin.tie(0); or fastio instead for problems with large io and/or tight TL.

So during Bubble Cup 13 - Finals [Online Mirror, unrated, Div. 1] a submission (94787679) using scanf/printf by one of my friends got TLE in the contest. After the contest I tried to add fread/fwrite for that submission and got Accepted in 405ms: 94813416. What surprised me is that the same solution can also get Accepted in 577ms (94878150) by using cin/cout with ios_base::sync_with_stdio(0);cin.tie(0);.

There are blogs testing cin/cout vs scanf/printf in the past, like this one. Results in that blog probably still hold for those compilers, however the same experiments on the new 64-bit msys2 G++ 9.2.0 complier seem to yield bizarre results: 94881344 using cin/cout runs in 140ms while 94881282 using scanf/printf runs in 404ms.

We had detective blogs like this one in the past, so I wonder if anyone has got a clue about what's going on.

Edit: Using the same methodology as the above blog, that is, by including <stdio.h> first, we have 94890173 which also runs in 140ms.

  • Vote: I like it
  • +19
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by q234rty (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by q234rty (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by q234rty (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by q234rty (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it -9 Vote: I do not like it

ios_base::sync_with_stdio(0);cin.tie(0); welcome you! So use only cin/cout if you are a C++ player