Блог пользователя deepanshujain088

Автор deepanshujain088, история, 3 года назад, По-английски

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.

  • Проголосовать: нравится
  • +17
  • Проголосовать: не нравится

»
3 года назад, # |
Rev. 3   Проголосовать: нравится +38 Проголосовать: не нравится

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 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

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

»
3 года назад, # |
  Проголосовать: нравится -8 Проголосовать: не нравится

add cout.tie(0);