time.sleep accuracy
Rob Hooft
rob at hooft.net
Tue Sep 19 02:32:40 EDT 2000
>>>>> "DB" == David Bolen <db3l at fitlinxx.com> writes:
DB> Peter Hansen <peter at engcorp.com> writes:
>> Is the latter statement more than just theory? I strongly doubt that
>> Windows, for example, would consistently come in any closer than +/- ten
>> or twenty milliseconds. Maybe on a real-time operating system.
DB> Well, it's definitely OS-dependent, since the implementation of
DB> time.sleep() is different by OS. Under Windows, it's using select(),
DB> so it depends on the native select() implementation's resolution, and
DB> obviously the resolution of the system scheduler in waking up a task
DB> that is ready to run. But at least under WinNT, it seems to be
DB> awfully good - at least better than I would have expected.
>> I have not had opportunity to test either way...
DB> Different parts of the time module have different resolutions, and as
DB> it turns out although I'm not sure if the select() granularity is
DB> documented, you can run some rough tests...
DB> The time.time() uses C library functions to establish the time, and
DB> could have fairly limited resolution - from some brief tests under
DB> WinNT 4.0 for example I get about a 10ms resolution on time.time(),
DB> but presumably that may also differ by platform.
DB> But time.clock() is designed to return the highest resolution of the
DB> platform, and on that same WinNT system it uses the performance
DB> counter, which for me yields resolution just under 1uS. In practice,
DB> a tight while 1: print time.clock() was running fast enough to show
DB> sub-ms resolution.
Huh? Sorry, but this is not a bit, but VERY platform dependent:
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":
>>> time.clock(); time.sleep(5); time.clock()
0.13
0.13
i.e.: time.sleep does not take any significant CPU time (as expected).
Regards,
--
===== rob at hooft.net http://www.hooft.net/people/rob/ =====
===== R&D, Nonius BV, Delft http://www.nonius.nl/ =====
===== PGPid 0xFA19277D ========================== Use Linux! =========
More information about the Python-list
mailing list