Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using scanf() in C++ programs is faster than using cin?

I don't know if this is true, but when I was reading FAQ on one of the problem providing sites, I found something, that poke my attention:

Check your input/output methods. In C++, using cin and cout is too slow. Use these, and you will guarantee not being able to solve any problem with a decent amount of input or output. Use printf and scanf instead.

Can someone please clarify this? Is really using scanf() in C++ programs faster than using cin >> something ? If yes, that is it a good practice to use it in C++ programs? I thought that it was C specific, though I am just learning C++...

like image 524
zeroDivisible Avatar asked Jun 25 '09 03:06

zeroDivisible


People also ask

Is scanf printf faster than Cin Cout?

Yes, scanf/printf are faster than cin/cout.

Is gets faster than scanf?

getchar is usually much faster than scanf.

Why Cin Cout is slower than printf?

The speed difference is largely due to the iostream I/O functions maintaining synchronization with the C I/O functions. We can turn this off with a call to std::ios::sync_with_stdio(false); By default standard C++ streams are synchronized to the standard C stream after each input/output operation.

What are the advantages of cout and cin over printf and scanf?

There are multiple benefits of using cout and cin instead of printf() and scanf(). 2. They use overloaded operators << and >> instead of predefined functions so multiple calls to these can be made in the same statement through cascading. 3.


2 Answers

Here's a quick test of a simple case: a program to read a list of numbers from standard input and XOR all of the numbers.

iostream version:

#include <iostream>  int main(int argc, char **argv) {    int parity = 0;   int x;    while (std::cin >> x)     parity ^= x;   std::cout << parity << std::endl;    return 0; } 

scanf version:

#include <stdio.h>  int main(int argc, char **argv) {    int parity = 0;   int x;    while (1 == scanf("%d", &x))     parity ^= x;   printf("%d\n", parity);    return 0; } 

Results

Using a third program, I generated a text file containing 33,280,276 random numbers. The execution times are:

iostream version:  24.3 seconds scanf version:      6.4 seconds 

Changing the compiler's optimization settings didn't seem to change the results much at all.

Thus: there really is a speed difference.


EDIT: User clyfish points out below that the speed difference is largely due to the iostream I/O functions maintaining synchronization with the C I/O functions. We can turn this off with a call to std::ios::sync_with_stdio(false);:

#include <iostream>  int main(int argc, char **argv) {    int parity = 0;   int x;    std::ios::sync_with_stdio(false);    while (std::cin >> x)     parity ^= x;   std::cout << parity << std::endl;    return 0; } 

New results:

iostream version:                       21.9 seconds scanf version:                           6.8 seconds iostream with sync_with_stdio(false):    5.5 seconds 

C++ iostream wins! It turns out that this internal syncing / flushing is what normally slows down iostream i/o. If we're not mixing stdio and iostream, we can turn it off, and then iostream is fastest.

The code: https://gist.github.com/3845568

like image 145
nibot Avatar answered Oct 07 '22 17:10

nibot


http://www.quora.com/Is-cin-cout-slower-than-scanf-printf/answer/Aditya-Vishwakarma

Performance of cin/cout can be slow because they need to keep themselves in sync with the underlying C library. This is essential if both C IO and C++ IO is going to be used.

However, if you only going to use C++ IO, then simply use the below line before any IO operations.

std::ios::sync_with_stdio(false); 

For more info on this, look at the corresponding libstdc++ docs.

like image 43
clyfish Avatar answered Oct 07 '22 17:10

clyfish