My friend's friend made a function for inputing unsigned integers. It's faster than scanf and much faster than cin. But I made a small change and I think it's a bit faster now. But I'm not sure. Here's the old function:
1 2 3 4 5 6 7 8
int scanInt ()
{
char c;
while (!isdigit (c = getchar ()));
int ret = c - '0';
while (isdigit (c = getchar ())) ret = (ret * 10) + (c - '0');
return ret;
}
It could be faster or it could be slower. My guess is that the first one is faster, if there is a difference at all. The difference is probably so small that it doesn't matter. The first function is less confusing so I would prefer that one.
What makes you think "returning" the value using a pointer could be faster than using return semantics (passing it back in a register)? In any case, this falls into the realm of micro-optimizations which you should stay away from unless the performance really matters and you know exactly what you're doing.
but I'm curious about which one is faster.
Speed measurements are done by measuring speed, not by theorizing.
More importantly, your version neither handles EOF nor negative numbers correctly.
@airman9519 Yes, helios is right - I should have made it clear that these were just timings of your code as written, sorry.
What he's trying to say is that the speed of execution will be limited by the IO access, so the timings are pretty meaningless. In fact, the 1st version is probably faster if you remove the I/O limitation.
If you want to give it a go, I timed the code by using ::QueryPerformanceCounter()