Comparing Input Functions for SPOJ input intensive problem : INTEST

Feroz Ahmad bio photo By Feroz Ahmad Comment
I tried the problem using 4 methods, results of which I have tabulated as following :
Method Time Comparision
Cin  2.72s SLOWEST 
scanf  0.66s  SAFE
cin with sync_with_stdio(false) 0.54s  NICE
getchar_unlocked() 0.11s  FASTEST
If you require fast IO, always use getchar_unlocked(), but remember, with great power comes great responsibility. getchar_unlocked() is not thread safe, getchar_unlocked is a thread unsafe version of getchar( ), and as it does not check for any locks for threads accessing stdin. In short, you can use for normal integer inputs and when having large Input requirements. Code Example using getchar_unlocked()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstdio>
using namespace std;
#define gc getchar_unlocked
void scan(int &input) //for int only
{
    input = 0;
    register int c = gc();
    for(;(c<48 || c>57);c = gc()); //for omitting spaces or any other character (other than number)
    for(;c>47 && c<58;c = gc()) {input = (input<<1) + (input<<3) + c - 48;} //reading of the number
}
int main()
{
    int n,k,i,ctr=0,x;
    scan(n);
    scan(k);
    for(i=0;i<n;i++)
    {
    scan(x);
    if(x%k==0)
    ctr++;
    }
    cout<<ctr;
    return 0;
}
comments powered by Disqus