0

I am trying to solve one codechef practice problem subtraction game 1 with c programming and Code Blocks as IDE. I found one way of reading input faster than scanf() function but when I run my program I am getting error "undefined reference to 'getchar_unlocked' error". Can you guys tell me what I am doing wrong and is there any other method for reading input faster?

#include<stdio.h>
inline int fastread()
{
        int noRead=0;
        char p=getchar_unlocked();
        for(; p<33;) {
                p=getchar_unlocked();
        };
        while(p>32) {
                noRead = (noRead << 3) + (noRead << 1) + (p - '0');
                p=getchar_unlocked();
        }
        return noRead;
};
unsigned int gcd(unsigned int a, unsigned int b)
{
    if (b == 0)
       return a;
    else
       return gcd(b, a % b);
}
int main()
{
    int t,i,answer=0;
    unsigned int n;
    t = fastread();
    while(t--)
    {
        n = fastread();
        unsigned int a[n];
        for(i=0;i<n;i++)
            a[i]=fastread();
        answer = gcd(a[0],a[1]);
        for(i=2;i<n;i++)
            answer = gcd(a[i],answer);
        printf("%u\n",answer);
    }
    return 0;
}
Jayesh Ahir
  • 51
  • 1
  • 9
  • I think `getchar_unlocked` is deprecated in Windows because it is thread unsafe version of `getchar()`.Sp might be it's not available in code block library. Unless speed factor is too much necessary, try to avoid getchar_unlocked. – Jayesh Bhoi Mar 29 '14 at 08:02
  • probably someone should ask MS why they don't support POSIX? – mfro Mar 29 '14 at 08:10
  • @Jayesh That means i can not use getchar_unlocked. can you suggest any other faster way for reading input? – Jayesh Ahir Mar 29 '14 at 08:15
  • @JayeshAhir I say no you can't.But you can use `scanf` or `getchar() ` to read single character. – Jayesh Bhoi Mar 29 '14 at 08:19
  • I think you are on the wrong track. Your problem doesn't seem to be that input is slow because the input stream might be locked, but because you are reading character by character. Change your design to use `fgets` or similar in to read in data in larger chunks. In any case this looks a lot like premature optimization to me. Do a detailed analysis of performance needs and bottlenecks first. But before that please learn a bit more about the different IO and string-to-number-conversion functions that the standard library offers. – Jens Gustedt Mar 29 '14 at 08:30
  • @Jayesh i know that i can use scanf but i submitted my solution in codechef with scanf and then i submitted solution with this fastread function using getchar_unlocked there is a difference in time as you can see from this [codechef] http://www.codechef.com/status/AMSGAME1,jakesahir – Jayesh Ahir Mar 29 '14 at 08:36

1 Answers1

2

Quoting this answer on SO

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

getchar_unlocked which has less overheads as compared to scanf or cin is not a standard feature of c or c++ , you can always use getchar() for that purposes here .

or you can write a function getchar_unlocked() to return the values of getchar() for on machine testing purposes if you are bound to use it in your online question .

Community
  • 1
  • 1