Fun with timers(2)

So I wrote a number of test programs. Each program uses a ‘before’ and ‘after’ timestamp retrieved by gettimeofday().

Regular usleep

Let’s start with plain usleep(). The results:

Plain usleep(10000): lowest=10000us, highest=25512us, average=10200us

That’s what we kind of expected. Especially after the previous post..

Adaptive usleep

OK, so lets be clever and instead of sleeping 10000us each time we calculate if the previous usleep() was too long. If so, deduct the extra time from the next interval. If the usleep() was too short, we add the missing time to the next interval. The results:

Adaptive usleep(10000): lowest=11us, highest=33054us, average=10100us

Wow, as you can see the timing interval goes all over the place but the average is slightly better!


It still sucks but on average we’re getting closer to the holy grail of 10000us!