6

Slowdown in twice with ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); in...

 2 years ago
source link: http://codeforces.com/blog/entry/96702
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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,107)[0,107):

#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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK