dmkz's blog

By dmkz, history, 3 years ago, In English

Hello! I want to show you the real life example where using of next features:

ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);

leads to slowdown of the program, written in C++, in two times.

Example is quite simple — just print binary representation of all numbers in range $$$[0,10^7)$$$:

#include <bits/stdc++.h>
using namespace std;
int main() {
    //ios::sync_with_stdio(false);
    //cin.tie(0); cout.tie(0);
    char buf[33]={0};
    buf[32] = '\n';
    for (int i = 0; i < 10'000'000; i++) {
        for (int bit = 30; bit >= 0; bit--) {
            buf[30-bit] = ((i >> bit) & 1) ? '1' : '0';
        }
        cout << buf;
    }
}

UPD. When I replaced buf[32] = '\n'; by buf[31] = '\n';, the situation wasn't changed.

If we run this example in codeforces custom invocation, then we will see Used: 1840 ms, 1184 KB. When we will uncomment speedup of C++ input/output, then we will see Used: 3759 ms, 1188 KB. It's a trap!

UPD2. AsGreyWolf suggested to change compiler to GNU G++20 11.2.0 (64 bit, winlibs) from GNU G++17 9.2.0 (64 bit, msys 2) and it resolved slowdown. Any another GNU compiler provides slowdown.

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