which is faster cin,cout/scanf,prin

which is faster cin,cout/scanf,prinf and why?
how can i reduce the compilation and execution time of a program.

just google it the first link will be this - http://stackoverflow.com/questions/18048946/why-is-scanf-printf-faster-than-cin-cout

I hope you already know, that we can use stdio along with iostream in C++. That means, we can use both scanf and cin for input in our program and even intermix calls to them. But, as both libraries have different implementations, they need to have separate buffers. But having separate buffers leads to a problem, we won’t be able to intermix calls to scanf and cin. We could only use one of them in our program.

This problem could be resolved by synchronizing iostream with stdio. We make iostream buffers small, and leave most of the buffering to stdio. As we have to make system calls to buffer from streams, normally we prefer big buffers so that we need less system calls. But, having small buffers would be a big overhead as it leads to more system calls.

That’s why cin is slow, it makes lots of system calls to buffer from stream. By default, iostream always wastes time synchronizing it’s buffers with stdio buffers. But, this could also be resolved. If you are going to use only iostream, you can turn this sync between iostream and stdio buffers off by calling:


For your second question, i think compilation time is not a matter of tension for small programs. But for bigger ones, you can read the answers to this question.

Execution time is what we have to be more worried about. Using better algorithm can make our program execute much faster. For example, if we have to check if NUM is prime or not. We can just check if any number from 2 to (NUM-1) is divisible by it. But this is a bad way to do it. As it could be made much faster by only checking if NUM is divisible by any number from 2 to sqrt(NUM-1). Execution time could also be improved by using better input/output functions as explained above.

further reading: relation of iostream with stdio.