When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

lbm364dl's blog

By lbm364dl, history, 3 years ago, In English

I'll leave it short. I got time limit because of big input so I started submitting some IO variations. I also decided to compare with using scanf, which should be faster, but for some reason using cin with ios_base::sync_with_stdio(false) shows about 400ms (120387767) while using scanf shows about 750ms (120392401). Am I doing something wrong? Why is scanf variant slower here?

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

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Once i also faced this issue in a problem with tight limits.
Use this for fast I/O:(only for C++ lang)
ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

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

I don't know if this is the reason for your slowdown, but sometimes compiler variants can have issues with format string parsing.

Here's one thread right here on codeforces which shows how gcc 6.2 C++14 was slower for printf and scanf, and some details on how the author tried to do something about it : <<< BLOG ENTRY 47180 BY LGM halyavin >>>

»
3 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Have a look at this.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Ok so it seems it was a 64 bit compiler issue as submitting on C++17 shows 400ms which is expected. I still don't know why it's like that (changing to an earlier version of the compiler as suggested by oversayan's comment doesn't show a significant difference from 400) but at least I know a fix. Thanks.