How is scanf("%d",&x) faster than cin>>x?

I was participating in an online algorithm challenge.
It required me to take large inputs at a time.
I could not complete the challenge using "cin" for input but I was successful with scanf();
Why does cin take so long time as to change the order of the algorithm?
Can you please explain the difference in working of cin and scanf that it changes the order of the algorithm...
Last edited on
You could try turning off the standard libs synchronisation with stdio like this:

sync_with_stdio(false);

http://www.cplusplus.com/reference/iostream/ios_base/sync_with_stdio/

That is supposed to improve performance because the standard libs do extra work to ensure that all the calls can interoperate with fprintf() and fscanf().
cin is also tied to cout, which imposes a further performance impact.
Last edited on
Topic archived. No new replies allowed.