Slow scanf/printf for the new 64-bit C++ compiler?

Revision en18, by q234rty, 2020-10-08 10:10:12

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.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en18 English q234rty 2020-10-08 10:10:12 2 Tiny change: 'oing on.\nEdit: Us' -> 'oing on.\n\nEdit: Us'
en17 English q234rty 2020-10-08 10:09:58 150
en16 English q234rty 2020-10-08 10:02:50 16
en15 English q234rty 2020-10-08 10:00:12 6 Tiny change: 'sion:94881344] using sc' -> 'sion:94881282] using sc'
en14 English q234rty 2020-10-08 09:57:48 1 Tiny change: 'still holds for those' -> 'still hold for those'
en13 English q234rty 2020-10-08 09:55:41 0 (published)
en12 English q234rty 2020-10-08 09:55:29 3 Tiny change: '.\n\nWe have detective' -> '.\n\nWe had detective'
en11 English q234rty 2020-10-08 09:54:23 1 Tiny change: 'with largeio and/or ' -> 'with large io and/or '
en10 English q234rty 2020-10-08 09:54:00 1 Tiny change: 'argeio and /or tight ' -> 'argeio and/or tight '
en9 English q234rty 2020-10-08 09:53:44 4
en8 English q234rty 2020-10-08 09:53:31 2 Tiny change: 'ompiler.\nUse cin/' -> 'ompiler.\n\nUse cin/'
en7 English q234rty 2020-10-08 09:53:19 45
en6 English q234rty 2020-10-08 09:52:33 1 Tiny change: 'submission([submissi' -> 'submission ([submissi'
en5 English q234rty 2020-10-08 09:52:02 2 Tiny change: '/57745).\n\nResults ' -> '/57745).\nResults '
en4 English q234rty 2020-10-08 09:51:45 9
en3 English q234rty 2020-10-08 09:50:33 307
en2 English q234rty 2020-10-08 09:44:16 27
en1 English q234rty 2020-10-08 09:42:45 836 Initial revision (saved to drafts)