Wednesday, 15 February 2012

c++ - Logic behind Fast input -


Can someone explain the logic behind this fast input? I know that faster than scanf.

  int scan () {int ip = getchar_unlocked (), ret = 0; (; IP & lt; '0' || IP> '9', IP = getchar_unlocked ()); Ret = ret * 10 + ip-'0 '; (; IP> =' 0 '& amp; IP & lt; =' 9 '; ip = getchar_unlocked ()); Return writ; }    

unlock is here to avoid locking Input file (Problems possibly occur if multiple threads are being read from the same input)

Perhaps it is possible that 90% of the benefits of using other getchar And it's probably only marginally better than scanf . Obviously, scanf is also an overhead in parsing the format string, which may be slightly overhead.

The rest of the code just does not leave anything "which is not a digit", then read a decimal number in ret , when the digit is a non-digit

To read a large number of inputs, I suggest using the fread (or mmap or MapViewoOfFile ) if the system is known Is to use a pointer-based method to load large amounts of input data into a buffer and then leave it on a non-point () This is a "safe" thing to believe). It is highly likely that it is faster than the above code.

No comments:

Post a Comment