Cin-Cout vs Scanf-Printf

Regular competitive programmers face common challenge when input is large and the task of reading such an input from stdin might prove to be a bottleneck. Such problem is accompanied with “Warning: large I/O data”.

Let us create a dummy input file containing a line with 16 bytes followed by a newline and having 1000000 such lines, making a file of 17MB should be good enough.

// Creating a dummy file of size 17 MB to compare 
// performance of scanf() and cin()
$ yes 1111111111111111 | head -1000000 > tmp/dummy

Let us compare the time taken to read the file from stdin (get the file from disk to stdin using redirection) by using scanf() versus cin.

// Filename : to test the 
// We redirect above created temp file 
// of 17 MB to stdin when this program 
// is run.
using namespace std;
int main()
    char buffer[256];
    while (cin >> buffer)
    return 0;

Please sign in to leave a comment.