14

What is the difference among these three input functions in programming language. Do they input in different ways from each other?

1.getchar_unlocked()

 #define getcx getchar_unlocked

 inline void inp( int &n ) 
 {
    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;
  }   

2.scanf("%d",&n)

3.cin>>n

Which one takes least time when input the integers?

I use THese header files in c++ where all 3 cased run in c++;

  #include<iostream>
  #include<vector>
  #include<set>
  #include<map>
  #include<queue>
  #include<stack>
  #include<string>
  #include<algorithm>
  #include<functional>
  #include<iomanip>
  #include<cstdio>
  #include<cmath>
  #include<cstring>
  #include<cstdlib>
  #include<cassert>
akdom
  • 32,264
  • 27
  • 73
  • 79
Anil Arya
  • 3,100
  • 7
  • 43
  • 69

2 Answers2

29

Two points to consider.

  1. getchar_unlocked is deprecated in Windows because it is thread unsafe version of getchar().

  2. Unless speed factor is too much necessary, try to avoid getchar_unlocked.

Now, as far as speed is concerned.

    getchar_unlocked > getchar

because there is no input stream lock check in getchar_unlocked which makes it unsafe.

    getchar > scanf

because getchar reads a single character of input which is char type whereas scanf can read most of the primitive types available in c.

    scanf > cin (>> operator)

because check this link

So, finally

getchar_unlocked > getchar > scanf > cin
Community
  • 1
  • 1
Sobhagya Mohanty
  • 628
  • 1
  • 11
  • 24
  • I dont get this comparison : **getchar_unlocked > getchar > scanf > cin**, because except `cin` (which is an object), all others are functions. Comparing functions speed makes sense. However, how can you compare functions with an object? It doesn't even make sense. You can however compare these functions with various functions `cin` support such as `operator>>` overloads and `read()` which have different tradeoffs, e.g `read()` is faster than `operator>>` but it doesn't format the buffer. – Nawaz Aug 14 '15 at 06:13
  • @Nawaz the answer is with the context of question where it clearly states the comparison is between the functions. – Sobhagya Mohanty Aug 15 '15 at 13:00
  • You're not comparing functions, is what I'm saying. – Nawaz Aug 15 '15 at 13:05
  • 1
    Just to clarify. All the above comparison are between reading functions available. Althrough I didn't exclusively mentioned operator>> function available in cin object. – Sobhagya Mohanty Aug 16 '15 at 17:32
  • If you've not mentioned it, then your post is misleading. – Nawaz Aug 16 '15 at 17:49
4

I had a problem in codechef that had to input many integers abd found out that the char_unlocked() is faster than scanf which is faster than cin

sai krishna
  • 49
  • 1
  • 2
  • One problem which explictly states that "Dataset is huge, use faster I/O methods.": [http://uva.onlinejudge.org/external/124/12440.html](http://uva.onlinejudge.org/external/124/12440.html) – thiagowfx May 26 '13 at 03:20