Improved speed of the program with getchar_unlocked()

when I was solving Chef and Right Triangles and INTEST, I have observed ( learned from others code ) that getchar_unlocked() is improving the reading speed considerably!

Screenshot from 2014-04-25 22:14:06Screenshot from 2014-04-25 22:26:42

I am attaching the getchar_unlocked() code for scanf("%d", n)


inline int get_int()
{
int n=0;
char c=0;
while(c<33)
c=getchar_unlocked();
while(c>='0'&&c<='9')
{
n=(n<<3)+(n<<1)+(c-'0');
c=getchar_unlocked();
}
return n;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s