# Timer sampling error

It seems to me that a number of recent questions go back to a
misunderstanding of the accuracy of timers. If I’m wrong, well please
understand that I am trying to help.

It is important to understand that just because the timer has ticked
since you last asked for the time does not mean that 1 timer tick worth
of time as passed. This is a classic sampling error problem known as
aliasing. Aliasing is not just a problem in graphics, if applies to any
kind of sampling.

Let’s say I have a timer that ticks once ever millisecond. I check it
and get a value of 10 then I go off and do something for a while and
check it again and it now says 11. How much time has passed? Well, if I
checked it the first time just after the timer ticked over to 11 and
then checked it again just before the timer ticked over to 13 then the
elapsed time may be 2 milliseconds minus some very small number that is
usually called epsilon. (I don’t know why it called epsilon, whenever I
asked the only answer I ever got was “I don’t know, it just is.”) or,
you could have checked the first time just before the timer ticked and
again just after the time ticked so the actual elapsed time could be a
very small value, called epsilon… while the measured time was 1
millisecond.

10 11 12 13±--------±--------±--------+
^-----------------^ real time is almost 2 ms, measured time is 1 ms
^-------^ real time is almost 1 ms, measured time is 0 ms
^-^ real time is almost 0 ms, measured time is 1 ms

This means that when you measure 1 millisecond on a 1 millisecond timer
the actual time you measured ranges from epsilon to 2 - epsilon or
approximately from 0 to 2. The problem can get worse. If you are taking
a sequence of independent measurements this kind of error can cause you
to be nearly 2 too small on one measurement and nearly 2 too large on
the next so that a series of measurements that should give you a
sequence of value that look like:

4, 4, 4, 4, 4, …

Can give values that look like

4, 4, 4, 5, 3, 4, 4, 2, 6…

Or at least something that looks a lot like that. The values can wander
all over the place, but an average taken over a large number of samples
will give you a value that is very nearly perfectly 4. It is easy to
mistake timers sampling error for a problem in you program. And, it you
let this error affect the actions of your program you may see the error
``	Bob Pendleton``