C++ Code not working due to ios_base::sync_with_stdio(false). Make this work or suggest other code for taking inputs in Fastest way

#include <iostream>
using namespace std;

inline void fast(int &n)
{
	n = 0;
	register int c;
	bool negative = false;
	c = getchar();
	while((c < 48 || c > 57) && c != '-' )
        c = getchar();
	if(c == '-')
    {
        negative = true;
        c = getchar();
    }
	while(c > 47 && c < 58)
	{
		n = (n<<1) + (n<<3)+ c - 48;
		c = getchar();
	}
	if(negative)
        n *= -1;
}

int main ()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int a,b;
    string s;
    fast(a);
    fast(b);
    cin >> s;
    cout << a << " " << b << "\n";
    cout << s;
    return 0;
}

I WROTE THIS CODE FOR FASTEST INTEGER INPUT USING getchar() AS WELL AS OTHER INPUTS FAST using CIN IN COMPETITIVE PROGRAMMING.

INPUTS :

1. 1 2

hello
// (inputs not in a single line)
// this works, here ‘enter’ is pressed before giving input ‘hello’.

2. 1 2 hello
// (inputs in a single line)
// not working after pressing ‘enter’. Has to give input again. It works perfectly well without "ios_base::sync_with_stdio(false);"
This is BREAKING the Code by removing the sync.

IS THERE ANY WAY TO SOLVE THIS AND MAKE 1 2 HELLO WORK
SO AS TO TAKE INPUT IN THE FASTEST WAY
or SUGGEST ANY OTHER METHOD or CODE !!!

!!! Also can you please suggest Fastest method for INPUT and OUTPUT of INTEGERS as well as STRINGS !!!

I think scanf & printf are sufficient.

Reading 4,000,000 positive integer values, each less than 10^7:

  • 1.55 sec - simple cin
  • 0.51 sec - scanf
  • 0.41 sec - cin with sync_with_stdio(false) and cin.tie(NULL)
    And getchar() is the Fastest Method to read INTEGERS, even FASTER than above mentioned cin and scanf.

So i was trying to make all inputs fast without the code breaking.

With scanf my machine took 0.286s.

You can refer to this link to see getchar() method is Fastest for integers
and cin with sync(false) is faster than scanf().
So i want to use both of these in a single code for better speed.

I had trouble using cin and scanf before. This may help you.

1 Like

As mentioned in the page @siddharth963 linked to, only use

ios_base::sync_with_stdio(false);

when you only use cin/cout or you only use scanf, printf, getchar…

scanf, printf, getchar and other methods from stdio.h were originally (and still are in C) the only methods to do input and output. With C++ came the new library iostream. They are two entirely different ways of reading input, that happen to read from the same place. To prevent unexpected behavior when using both in a program, iostream was made in a way that it communicates what it has read to the stdio.h functions. This communication is what you disbale using the sync_with_stdio(false) line.

That’s why cin/cout is normally slower than scanf/printf; it has to also do some things to make scanf and printf keep working. Without that it is faster, but then you can’t use both.

In general the IO speed is not the most important factor in determinging whether you get TLE or not. I rarely encounter a problem for which cin/cout where sync with stdio is turned on gives me TLE whereas with it turned off it gives AC (I did once, but that one should not have passed based on complexity). In general if a solution gets TLE with cin/cout, you need to improve the algorithm.

1 Like