Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast input output function

#define getcx getchar_unlocked
inline void inp( int &n )//fast input function
{
   n=0;
   int ch=getcx();int sign=1;
   while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=getcx();}

   while(  ch >= '0' && ch <= '9' )
           n = (n<<3)+(n<<1) + ch-'0', ch=getcx();
   n=n*sign;
}

Hi I have been using the above function for input in various coding contests but was never able to understand why is it fast. I know the logic but don't know the concept of it's fastness. For example what is this line doing "#define getcx getchar_unlocked" . Also I don't know any fast output function so is there any fast output function also

like image 932
user1205088 Avatar asked Aug 22 '12 12:08

user1205088


People also ask

How do I get fast input output?

It is often recommended to use scanf/printf instead of cin/cout for fast input and output. However, you can still use cin/cout and achieve the same speed as scanf/printf by including the following two lines in your main() function: ios_base::sync_with_stdio(false);

What are fast input output methods in C?

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.

What is fast input output method in Python?

In Competitive Programming, it is important to read input as fast as possible to save valuable time. Input/Output in Python can be sometimes time taking in cases when the input is huge or to output any numbers of lines or a huge number of arrays(lists) line after line.

What is fast IO?

Fast input and output in C++ ios_base::sync_with_stdio(false) - It toggles all the synchronization of all C++ with their respective C streams when called before cin/cout in a program. We make this function false(which is true earlier) to avoid any synchronization.


2 Answers

getchar_unlocked() is the thread unsafe version of getchar()The reason that getchar_unlocked() seems faster is that it doesn't check for any locks on the input stream from where it is supposed to fetch a character. So if another thread has locked the input stream, this thread is supposed to wait till lock count has come to zero. But this function doesn't care about it, thereby destroying synchronisation between threads.

But if you are sure that lack of synchronisation wont harm you, then this function might help you to be a bit faster.

Also, its advised that you can use it safely only when the invoking thread has locked stdin using flockfile() (or ftrylockfile()).

like image 150
Pavan Manjunath Avatar answered Oct 03 '22 23:10

Pavan Manjunath


The #define uses the preprocessor to make getcx be a short-hand for the function getchar_unlocked(), which is a non-locking character-reading function.

It's a bit awesome that you've competed in several coding contests without understanding this pretty basic piece of C.

The manual page I linked to above mentions putc_unlocked() which sounds like pretty much the same thing but for output.

like image 42
unwind Avatar answered Oct 03 '22 23:10

unwind