Fast I/O in C++ for Competitive Programming

If you are not new to competitive programming then you may have found that, some questions specify that,
Warning: Large I/O data, be careful with certain languages (though most should be OK if the algorithm is well designed) .

So, ever wondered why do they write this? Or what are various options to overcome this warning?.
Don’t worry Programmers Army is here to help you out and we will make this content very simple so that you can understand this very easily 😊

The reason for some of the questions of mentioning such warnings are:

Usually these questions want you to write a solution that reads input from standard input and write output to standard output. And at the backend these inputs are uploaded as test case files ( mostly .txt).

So, if these test case files contain huge number of inputs included, then for just reading that much inputs your program will take much higher time than expected and it would run out of time limit bounds. Hence, the creator of question specifies before hand that this question contains large input/outputs and you have to use fast input/output methods.

Indirectly they are saying you, to not use normal input output methods, as they will give unexpected results and time bound error.

So, what to use for reading fast inputs?
Now, coming to fast input/output in C++, you may be wondering what can we use instead of cin and cout, for faster input/outputs….
Well, then I would like to bring your attention towards scanf() and printf(), These two magical functions of C language are actually much faster than cin and cout.

This is simple code in C++, which takes array as input and outputs the same.

Execution time : 0.34s (it may vary depending on your system)

This is same code in C, using scanf(), and printf(), and guess what execution time for this code is : 0.01s (it may vary depending on your system) isn’t that great?

But wait C++ lovers, we have something great for you which can make cin and cout more faster (or equivalent to) than scanf() and printf().

Adding below 2 lines in the start of your main function, will make cin and cout, much more faster than before.

Why it makes cin and cout faster?

Toggle synchronization with cstdio streams [static]. Toggles on or off synchronization of all the iostream standard streams with their corresponding standard C streams if it is called before the program performs its first input or output operation.
If called once an input or output operation has occurred, its effects are implementation-defined. By default, the parameter of above function is true, and we have to make it false, for not toggling.

tie() is a method which simply guarantees the flushing of cout before cin accepts an input.
So, by passing it NULL pointer we are basically limiting tie() method to function, on given inputs which ultimately makes inputs to our code faster.
So, after adding above 2 line in start of my main function, we achieved to get execution time closer to what we got from scanf() and printf(), Voila !!

1. If you are including separate header files for using various data structure and algorithms that are pre-implemented in Standard Template Library(STL), then we have a gift for you. You can just include this one header file in your C++ program and you can now use any Data Structure and Algorithm of STL,

And, guess what this will also make your code to run faster, think why? yes, just because compilation time will be reduced due to just one header file.

2. You can use “\n” instead of endl, it will also make your code faster. Why?

Because,”\n” just takes your cursor to new line whereas endl, take cursor to new newline as well as flushes the output stream everytime.
So, this make “\n” , a best option to use it.

3. If you want lightning speed, input in your code then read further:

Here, we are reading one character each time from input buffer and we will keep on reading until it is in ASCII range i.e. ( from 48 to 57 ).

If the number is negative we will multiply it with -1.


So that’s it for this article we will be coming up with our next article very soon till then keep learning, keep coding, keep reading and keep improving !!

Happy Coding 😊
By Programmers Army