52
Fast IO Optimization in C++
C++
C++ Language
C++11
Competitive Programming

Lets say you have take an input of 10^5 numbers, there are many methods

1. std::cin>>x


you can use std::cin which is a standard method to obtain input from STDIN how ever there are better methods

2. scanf("%d",&x);


scanf / printf are roughly thrice as fast as the cin/cout

3. FAST IO
void fastscan(int &x)
{
bool neg=false;
register int c;
x =0;
c=getchar();
if(c=='-')
{
neg = true;
c=getchar();
}
for(;(c>47 && c<58);c=getchar())
x = (x<<1) + (x<<3) +c -48;
if(neg)
x *=-1;
}

fastscan(x);


this is one of the fastest way to take an integer input from STDIN this is around 10 times faster than scanf() This is very useful in competetive programming

NOTE : You can add ios_base::sync_with_stdio(false); in main() function to make your cin adn cout faster

Author