deepanshujain088's blog

By deepanshujain088, history, 3 years ago, In English

I have submitted the same algorithm with different input streams (cin and scanf) , I used ios_base::sync_with_stdio(false);cin.tie(NULL); with the cin and cout but still got a high difference in speeds?

Can somebody explain??

The problem link that I submitted is this one-- https://codeforces.com/problemset/problem/368/B

and my two differnt solutions are of 111556695 and 111568920 on Mar/31/2021.

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

| Write comment?
»
3 years ago, # |
Rev. 3   Vote: I like it +38 Vote: I do not like it

the issue arises from the cout and not the cin

cout << endl; is know to make code slower so i swapped that out for cout << '\n'; and this here my submission with that, 155ms or so

then i just tried using printf instead of cout this and it runs in 109ms or so

so the conclusion is (debug more) dont cout << endl as endl flushes the output which takes extra time. well even then printf still outdoes cout and scanf still outdoes cin. unless someone else can make some changes with this code and prove me wrong

EDIT: i submitted both codes again in c++17 and cout with '\n' and printf (with cin as input) both ran in 77ms whereas cout << with endl ran in 340~ms.

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

    Thanks bro, I googled but didn't get anything on this. But now I get where is the problem. :)

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

This issue and It's solution is very well explained by Priyansh31dec, Here is the link to it. Hope this helps.

»
3 years ago, # |
  Vote: I like it -8 Vote: I do not like it

add cout.tie(0);

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

    Please tell me that you are trolling.

    In case someone takes this seriously, cout.tie(NULL) does literally nothing.