time.sleep accuracy

David Bolen db3l at fitlinxx.com
Tue Sep 19 13:39:32 EDT 2000


rob at hooft.net (Rob Hooft) writes:

> Huh?  Sorry, but this is not a bit, but VERY platform dependent:

Of course - I thought I was pretty clear about that in my response.
The time functions are OS-dependent and I was commenting on the
Windows version since the previous poster had "...not had an
opportunity to test..." - I did, at least for one platform :-)

> clock () 
>     Return the current CPU time as a floating point number expressed
>     in seconds. The precision, and in fact the very definition of the
>     meaning of ``CPU time'', depends on that of the C function of the
>     same name, but in any case, this is the function to use for
>     benchmarking Python or timing algorithms.
> 
> Note that this description does not say anything about "highest
> resolution". And on "Unix":

Hmm, looks like the manual is a little off in this case - the doc
string is slightly different (and from my glancing at the source, more
correct).  From my 1.5.2:

>>> print time.clock.__doc__
clock() -> floating point number

Return the CPU time or real time since the start of the process or since
the first call to clock().  This has as much precision as the system records.

>   >>> time.clock(); time.sleep(5); time.clock()
>   0.13
>   0.13
> 
> i.e.: time.sleep does not take any significant CPU time (as expected).

True - and apparently under Unix, time.clock() isn't suitable to
trying to guage the real wall time that your request for 5 seconds
took.

But just for reference, under Win32 (excluding 64-bit platforms as of
2.0), time.clock() is not based based on the actual clock() function,
but uses the high-resolution performance counter, and I believe
returns real time, not CPU time.  So I think that at least on the
Windows platform, it's suitable to use as I did - to get a rough gauge
of precision of time.sleep().

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list