Difference caused in run time by scanf/printf when used in place of cout/cin

I just want to know whether using scanf/printf instead of cout/cin can cause significant difference in the run time?

I was solving a question CHMOD, and was getting TLE when I used cout/cin. But when I shifted my I/O operations to scanf/printf, I got an AC.

[The part below is totally unrelated to question and reflects my current state of mind].

When I was using cout/cin, my approach to the problem wasn’t wrong, but then also I had to deal with TLE. And when I or any one gets a TLE, it directly signifies that there is something wrong with the run time complexity of the code. Thus the person tries to optimize his code. And after a lot effort if he gets nothing, he either goes to the editorial or read someone elses code or in extreme cases gets depressed/demoralized/frustated and leaves the problem.

I went through the third case, as after going through few accepted codes, I wasn’t able to figure out what was going wrong with my solution.

If I code in JAVA, I always know that I should never use Scanner class for I/O, as it is mentioned everywhere. But in C++, it is no where mentioned that “it is highly recommended to use scanf/printf for I/O instead of cout/cin”.

P.S - I just want to say that such small things should be taken care of by the judging machine or it should be mentioned in the question statement or somewhere else to use scanf/printf instead of cout/cin

I might be wrong about many things I have written here, so please forgive me if I went wrong somewhere.

I don’t see any AC on that task for your account but anyway yes there is a difference. To preserve some comptability between cin/cout and scanf/printf cin and cout have some drawbacks, they have buffered reading and writing which makes them slow(only in newer versions). If you are only using cin/cout do
ios_base::sync_with_stdio(false) before reading or writing something, this should make cin/cout the same speed if not faster.

Later Edit: This only matters with gcc 4.8.1 on codechef.

2 Likes

Yes, scanf/printf is about 5 times faster than cin/cout. Have a look here, for a deeper explanation.

1 Like

No, it is the other way round, if you understand the mechanism properly.

https://www.quora.com/C++-programming-language/Is-cin-cout-slower-than-scanf-printf

1 Like

Same case happened to me also in that contest…When i used cin/cout it gave me TLE but when i switched to faster I/O it got AC…I dont know why it happened. There should be time limit for different language. If you are using cin/cout and there are a lot of input data then the time limit should be increase.

whats the header file for scanf and printf in gcc 4.8.1 compiler??codechef is not accepting #include
an error pops up showing “prog.cpp:2:16: fatal error: stdio: No such file or directory #include ^ compilation terminated.”

it’s written in FAQ - http://www.codechef.com/wiki/faq#Why_do_I_get_a_Time_Limit_Exceeded

Plain vanilla scanf is faster than cin, because of synchronization delay. Synchronization needs to be turned of!!

It’s your problem when you are using slow constructs in your solution, especially when it is written in FAQ - http://www.codechef.com/wiki/faq#Why_do_I_get_a_Time_Limit_Exceeded

try… #include or #include<stdio.h>

1 Like