+ 2
What is getchar_unlocked()?
I recently made a profile on CodeChef and found out that many people use getchar_unlocked function for inputs there during competitions as it is faster in some way. It was described as a function for fast I/O... Can anyone explain its functioning to me in detail and how is it faster than cin?
15 Answers
+ 3
lets divide this question into parts so that u understand every bit of it:
WHAT IS getchar_unlocked() AND why it is used for?
In a POSIX environment, standard input/output functions like getchar() use mutexes to prevent concurrent access to the input buffer by multiple threads, which makes them thread safe (synchronized). An unlocked version of such functions means they do not use mutexes and do not check whether a mutex lock has been set by another thread on the input buffer,
Now few terms that we have encountered above that needs to be clear:
i)thread
ii)POSIX
WHAT ARE THEY:
THREAD: I Start with an example .suppose ur college has planned to organize an annual fest . To make the fest a great success it breaks the work into different pieces and assign different piece of work among different people .They all complete their own piece of work and the fest becames a great success.
Similarly an big program can be divided into different parts called threads and each thread can work independent of the other .when there are more than one thread in a program it is called a multithreaded program.
Now what getchar does is that it prevents concurrrent access of the input buffer by different threads.That is two different threads are not allowed to access the input buffer at the same time.This is needed to ensure smooth functioning of the program.what getchar_unlocked() does that it does not check if multiple thread are trying to access the input buffer simultaneously or not . so it boosts time .
+ 4
I can not. But found this: https://stackoverflow.com/questions/1042110/using-scanf-in-c-programs-is-faster-than-using-cin
+ 4
by setting the ios flag sync_with_stdio to false.
basically iostream needs to sync with stdio.h to do some magic. But if you are not going to use any c style headers that deal with streams you can turn this sync off.
I think thats the reasoning behind it
+ 4
someone did tests on stackoverflow link.
the second explains the theory i believe
+ 3
@jay
Thanks for your efforts...
+ 3
Apparently cin can be made much.much faster
+ 3
How?
+ 3
Ill have to look more into io for this...
+ 3
my answer is big therefore it is not geeting uploaded therefore i am breaking my answer into two parts
+ 3
POSIX:C++ does not contain any built-in support for applications. Instead, it relies entirely upon the operating system to provide this feature.
if we assume that we are working on Linux OS and we are going to write multi-threaded C++ program using POSIX.it is simply a way to declare multithreaded function.
WHY DO WE NEED /USE getchar_unlocked()?
i)it increases performance since there is an absence of overhead caused by using mutual exclusion. That is why getchar_unlocked() offers faster performance than getchar(). It's perfectly safe to use unlocked functions in a single-threaded application where performance is an important factor, otherwise it is recommended not to use getchar_unlocked(), especially in a multithreaded application.
+ 2
@NIKHIL
Thank You Very Much!
Your answer is so explanatory!
+ 2
@NIKHIL
Can you also explain how to use it in a simple code snippet?
Sorry to disturb đ
đ¶
Thank You in Advance...
+ 1
@kinshuk
SYNOPSIS
#include <stdio.h>
int getchar_unlocked(void);
for more info check this link out:
http://www.mkssoftware.com/docs/man3/getchar_unlocked.3.asp
+ 1
@NIKHIL
Thank You very much!