# measuring 1/100th seconds, what function?

Bengt Richter bokr at oz.net
Wed Aug 11 02:56:33 CEST 2004

```On Tue, 10 Aug 2004 17:30:14 -0400, Peter Hansen <peter at engcorp.com> wrote:

>Jonas Kölker wrote:
>
>> I'm doing a Rubik's Cube timer, so I need a function to measure 100ths of
>> a second. I've browsed through the library reference of python.org, but I
>> didn't find anything that struck me as 'the perfect fit'. Either it was
>> system dependent, or it was too grainy (lo-res), or... something else.
>
>It usually helps to specify what you looked at, and perhaps why you
>didn't think it was acceptable, to avoid us retracing your steps...
>
>> so, I need a function which satisfies (in order of importance):
>>
>> 1. It can be used to measure time in the range of up to approximately 1
>> minute with an accuracy down to 100th-seconds (that's 60000~65536
>> different values). Better accuracy is a bonus, longer time-frame is a
>> bigger bonus.
>>
>> 2. it must run on any version of windows upon which python 2.4 can run
>
>These two constraints cannot be met simultaneously, I believe.
>Windows98 does not provide adequate resolution in the timer.
>
>If you are willing to forego Win98 and use only the real Windows OSes,
>then I think you get roughly (?) 10ms resolution already with
>time.time().  Linux probably gives 1ms resolution or better in pretty
>much all cases with time.time().
>
On windows I like to use time.clock()
For NT4 with python 2.3.2 (yeah I ought to upgrade both ;-)

>>> from time import time,clock
>>> min(filter(None,[-(time()-time()) for i in xrange(100000)]))
0.0099999904632568359
>>> min(filter(None,[-(clock()-clock()) for i in xrange(100000)]))
5.8666657594130811e-006

IOW, time apparently gets the NT4 basic os time slice delta (10ms),
whereas clock tries to get something better (and it's pretty good).
The filter call is because time()-time() makes a lot of zeroes.
clock() is fast enough not to require it. Not one zero in 100k, anyway.
>>> min([-(clock()-clock()) for i in xrange(100000)])
5.8666656741479528e-006

And that's not the minimum resolution of the timer clock() uses, it's
near the minimum time between two readings you can get from python.
Maybe some other loop mechanism can get two two samples faster and
then subtract them. A game for someone ;-)

Regards,
Bengt Richter

```