 # Fastest Input function?

im currently using a fast input function… but im wondering if there is faster …?
this is the ft:

``````inline int fast_inp()
{
int temp;
char c;
while(c<33)
{
c=getchar_unlocked();
}
while(c>33)
{
temp = temp * 10 + c - '0';
c = getchar_unlocked()
} return temp;
}
``````

also if someone can explain how this stat works : temp = temp * 10 +c - ‘0’; cz i dont understand how can we subtract ‘0’ to a char and add to int

I do not understand why are so many coders interested in time consumed by program…

I do not know if there is something faster (if judge is returning TLE, and you are using scanf, than you have bad algorithm complexity).

Your function above is reading input char by char and function wants to red int. So if you have 123, you know that this is (10*((10*1)+2)+3), in loo you will do

``````0 // initial value
0 // prepare for the first digit
1 // first digit
10 // prepare for next digit (x10)
12 // second digit (+2)
120 // prepare for next digit (x10)
123 // last digit (+3)
``````

so if you understand that, the last thing you need is to convert characters to digits and you can do this by substracting ‘0’, and even if you do not know ascii values of characters, you probably agree, that ‘0’ - ‘0’ = 0, and digits are in ascii in correct order, so ‘1’ = ‘0’ + 1 => ‘1’ - ‘0’ = 1 and so on…

2 Likes