How to optimize C++ code with faster I/O?

Hello everyone. I just joined Codechef about a month ago and I’m really enjoying the experience. But I guess the most frustrating situation arises with a TLE. The only fast I/O technique I’ve learnt is pasting the following code before my program. I code in C++:

std::ios::sync_with _stdio(false);

cin.tie(NULL);

I’ve seen a significant improvement in my program execution speeds, but I’ve hit another roadbloack. I still get a TLE even after using the above technique. So I would like to know where I can learn a few more I/O optimization techniques, be it a book, an external link or any other source of information. Thanks in advance!!

3 Likes

See brother,

What i learnt from codechef is Algorithm matters, if your algorithm is good enough you will not face TLE…

I think Primary Priority is Algorithm and then Fast I/O. SO focus more on efficient algorithm than Fast IO.

If you want to learn fast io then read the code from top submissions in any problem…!

BTW this is a template you can add for input and output:

#include< stdio.h >
#define gc getchar_unlocked
#define pc putchar_unlocked

/* For windows based system */
/* Replace getchar_unlocked with getchar */
/* Replace putchar_unlocked with putchar */


/**
Use :
[ lprint ] for printing long numbers to buffer
[ lscan ] for taking long integers as input from buffer
[ sscan ] for taking normal integers as input from buffer
**/


inline void lprint(long int a)
{
 int i=0;
char S[20];
while(a>0)
{
    S[i++]=a%10+'0';
a=a/10;
}
--i;
while(i>=0)
pc(S[i--]);
pc('\n');
}

inline unsigned long long uscan()
{
    unsigned long long n=0,c=gc();
while(c<'0'||c>'9')
c=gc();
while(c<='9'&&c>='0'){
n=n*10+c-'0';
c=gc();}
return n;
}

inline long int lscan()                 
{
    long int n=0,c=gc();
while(c<'0'||c>'9')
c=gc();
while(c<='9'&&c>='0'){
n=n*10+c-'0';
c=gc();}
return n;
}


inline  int sscan()                     
{register  int n=0,c=gc();
while(c<'0'||c>'9')
c=gc();
while(c<='9'&&c>='0')
    {
n=n*10+c-'0';
c=gc();
    }
return n;
}

Here is one submission link where i have implemented FAST IO just have a look:

http://www.codechef.com/viewsolution/5455880

Good Luck!! :slight_smile:

3 Likes

Include this fast input output function at the beginning of the code .

 inline void Scan_f(int a)
   {
  char c = 0;
  while(c<33)
  //c = fgetc_unlocked(stdin);
  c = getc(stdin);
 a  = 0;
 while(c>33)
 {
 a = a*10 + c - '0';
 //c = fgetc_unlocked(stdin);
 c = getc(stdin);
 }

and in place of scanf("%d",&t) ; you should use this function as Scan_f(&t) ; only.

I would like to make a remark here , in 99% cases problem will not demand fast IO methods , just use scanf and printf in C++ .

It is the beauty of the algorithms that make the code efficient .
so try to focus on your algorithms that IO methods .

as aptly mentioned by @rishabhprsd7 , if your program gets TLE on any online programming site, in most cases it is due to inefficient algorithm rather than slow i/o. Also, std::ios::sync_with _stdio(false); and cin.tie(NULL); are not that slow and will get you AC in almost all cases if your algorithm is efficient.
But, if you want to experiment, you can also use the following code segment for fast i/o I found on the web :
link Just add this at the top of your code and use normal cin or cout. No need to do any thing else

well as everyone said fast I/O is not much important but I disagree to that. It is necessary for you to know that there is a marked improvement in execution time by using getchar_unlocked and you must give it a try if your code is showing TLE.
I tried a problem using different I/O methods and here are the results for problem

  1. using cin :- time= 1.30

  2. using scanf :- time= 0.52

  3. using cin with
    std::ios::sync_with _stdio(false); :- time = 0.44

  4. using cin with
    std::ios::sync_with _stdio(false);
    cin.tie(NULL); :- time=0.41

  5. using getchar :- time = 0.34

  6. using getchar_unlocked :- time = 0.15

as you can see execution time went down from 1.30 to 0.15 which is in the case when problem does not involve large I/O. So you can yourself judge the necessity of fast I/O.

3 Likes

Yes, I too faced the same problem of TLE in my code. Then I looked for some solution which took me to the two lines added in the beginning of program , i.e std::ios::sync_with _stdio(false);

cin.tie(NULL); , but this too didnt worked and I again got a TLE. I would like to know if there is any other method for FAST I/O.

1 Like

thanks for the advice rishabhprsd7. I will now focus more on my algorithms

What is the unit of this, ms?