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++
2.1.
Tip 1
2.2.
Tip 2
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Fast input and output in Cpp

gp-icon
Basics of C++
Free guided path
9 chapters
99+ problems
gp-badge
Earn badges and level up

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. 

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:

#include <bits/stdc++.h>
using namespace std;
 
int main()
{ 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}

 

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

  1. Is Endl slower than “\n”?
    Yes, Endl flushes the output buffer while "\n" does not. If you want the buffer flushed frequently, use “\n”. If you do, use endl. Endl is actually slower because it forces a flush, which is unnecessary.
     
  2. 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.
     
  3. Why is printf faster than cout?
    Each has its own set of expenses. Depending on what you print, either option could be faster. Printf() must parse and act on the "format" string, which incurs a cost. Cout has a more complicated inheritance hierarchy and passes objects around.

Key Takeaways

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!

Previous article
Tuples in C++
Next article
Fast I/O in C++
Guided path
Free
gridgp-icon
Basics of C++
9 chapters
104+ Problems
gp-badge
Earn badges and level up
Live masterclass