Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Fast I/O in C++
3.
Tip 1
3.1.
Complete C++ template with Fast I/O:
3.2.
C++
4.
Tip 2
5.
Frequently Asked Questions
5.1.
Is scanf faster than cin?
5.2.
What is the fastest way to get input in C++?
5.3.
What are fast input-output methods?
5.4.
What is input and output in C++?
6.
Conclusion
Last Updated: Apr 21, 2024
Easy

Fast input and output in C++

Introduction

This article will discuss an amazing technique for the Fast I/O or Fast Input/Output in C++. This method reduces the input and output time of our C++ program and makes it more optimised. This method helps us to save time in programming contests. 

Fast input and output in C++

This method is widely used by most C++ Competitive programmers around the world. This technique is known as Fast I/O or Fast Input/Output.

Fibonacci Series in C++

Fast I/O in C++

As we all know, C++ is a backward compatible language, which means it supports the majority of the syntax of the C programming language.

For output and input in C programming, we use the printf() and scanf() functions, respectively.

In C++, we can take input using scanf() and output using cin and printf() and cout.

Many people recommend using scanf() and printf() instead of cin and cout to achieve fast input and output, but we can do the same with cin and cout by using these two lines inside the main function:

ios_base::sync_with_stdio(false);
cin.tie(NULL);

 

By default, cin is synchronised with stdio, which causes it to avoid any input buffering, resulting in a lot of overhead for every character. We can enable input buffering by including the following code at the top of your program:

std::ios base::sync with stdio(false);

 

After adding this line, the stream will now read in chunks instead of reading one character at a time.

So now the question is, why isn't it always turned on?

Both cin and scanf have their own buffers. If this was set to 'on' by default and you took input with cin, the buffer would contain more than intended because it reads in chunks. 

By using the Second line, we can untie the cin and cout streams:

cin.tie(NULL);

 

This connection between cin and cout means that when we use cin, cout must be flushed to provide the output, and when we provide the output, cin must be flushed to accept other input.

This synchronisation is useful in interactive problems where we need to use cin and cout repeatedly. Still, it is inefficient in competitive programming because it slows down the process for large I/O operations.

Tip 1

We can include all of the available Standard Template Library (STL) functions with a single include:

#include<bits/stdc++.h>

 

The header bits/stdc++.h is included to include all standard libraries. In other words, including bits/stdc++.h includes all standard libraries. It is very useful when you don't want to waste time including different header files. However, including this header file results in the inclusion of numerous unnecessary files, which increases compilation time.

Complete C++ template with Fast I/O:

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Tip 2

It is also recommended that you use cout<< "\n" to change the line rather than cout<<endl, because endl slows down the process by calling the flush stream.

Let's understand this with an example where we need to print numbers from 1 to 100:

Every time the code executes line 10, the output buffer is flushed. As a result, the buffer is flushed 100 times (once after printing each number).
 

Using “\n” would first fill the output buffer with all 100 numbers and then flush it once at the end of the program.

Must Read Swap Two Numbers Without Using Third Variable

Frequently Asked Questions

Is scanf faster than cin?

With synchronisation turned off, the above results show that cin is 8-10% faster than scanf (). This is likely due to the fact that scanf() interprets the format arguments at runtime and uses a variable number of arguments, whereas cin does so at compile time.

What is the fastest way to get input in C++?

Use std::ios_base::sync_with_stdio(false) and cin.tie(NULL) for faster input in C++.

What are fast input-output methods?

Fast input-output methods include sync_with_stdio and cin.tie(NULL) for input and cout with '\n' for output efficiency.

What is input and output in C++?

Input in C++ refers to receiving data from external sources like keyboard or files, while output refers to displaying data.

Conclusion

This article discussed amazing techniques for Fast I/O in C++ with some extra tips, including a special header file bits/stdc++.h header file and comparison of endl and \n.

Also check out this article - Pair in C++

Recommended Readings:

You can also consider our competitive programming course to give your career an edge over others!

Happy Coding!

Live masterclass