(scanf/printf) VS (cin/cout)

does scanf , printf take less time than cin,cout

as this(uses cin/cout) http://www.codechef.com/viewsolution/1371047 took 1.85 seconds

and this (uses scanf/printf) http://www.codechef.com/viewsolution/1371038 took around 0.54 seconds

Hello,

Yes, it does take significantly less time, and sometimes it can be the difference between an Accepted solution or the TLE status (Time Limit Exceeded)!!

As I usually say, there are no better doubts that the ones that are already cleared out, so, if you search the FAQ you will find that tip explicitly written down :smiley:

Bruno

EDIT:
PS: I’m sorry for my not referenced answer, here it is all you need to know:

2 Likes

You have answered your question yourself :smiley:

I will just add one thing, which I am not sure makes difference on codechef, but is important to know while using cout.

Don’t use std::endl for adding a newline, unless you want to flush the buffer. Use '\n' instead, as it is faster.

Reference : http://stackoverflow.com/a/8311177

use of getchar_unlocked() function can make the input more faster than scanf.i guess so…

we need to just tweak the code to receive all data types inputs using getchar_unlocked()…

1 Like

Yes, because cin wastes time synchronizing with scanf().
You can stop this by using std::ios::sync_with_stdio(false).
Have a look here for a deeper explanation.

1 Like

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