System Clock

Terry Reedy tjreedy at udel.edu
Thu Nov 6 22:47:19 EST 2003


"James Harriman" <v38zy at unb.ca> wrote in message
news:9SBqb.8942$R13.490724 at ursa-nb00s0.nbnet.nb.ca...
> Hi,
>
> I need to be able to measure a time interval in milliseconds on a
windows
> machine. I have tried using time.clock() but it appears to measure
time in
> seconds...Is there a way to measure time more precisely?

For floats, what matters is the resolution (precision), not the unit.
On AMDK2-385, Win 98:

>>> a=time.clock(); time.sleep(1); print time.clock()-a
1.01412863105 # correct, seconds

>>> time.clock()-time.clock()
-3.0171474548978949e-005
>>> time.clock()-time.clock()
-3.1847667571582861e-005
>>> time.clock()-time.clock()
-3.100957104607005e-005
>>> time.clock()-time.clock()
-2.933337799504443e-005
>>> time.clock()-time.clock()
-3.0171474577400659e-005
>>> time.clock()-time.clock()
-3.100957104607005e-005
>>> time.clock()-time.clock()
-3.1009571102913469e-005
>>> time.clock()-time.clock()
-2.9333378051887848e-005

This is reproducibly .03+ milleseconds (minus because earlier - later)
== 30 microseconds.

Now speed up a bit:
>>> tc = time.clock
>>> tc()-tc()
-2.8495281526375038e-005
>>> tc()-tc()
-2.7657185000862228e-005
>>> tc()-tc()
-3.017147452055724e-005
>>> tc()-tc()
-2.6819088532192836e-005
>>> tc()-tc()
-2.8495281526375038e-005
>>> tc()-tc()
-2.7657185000862228e-005
>>> tc()-tc()
-2.8495281526375038e-005
>>> tc()-tc()
-2.7657185000862228e-005
>>> tc()-tc()
-2.7657185000862228e-005

The average is about 28 microsecs, so time.clock lookup appears to be
measurebly about 1 microsecond, but anything much smaller would be
lost in the noise on this system.

(Oddly, I also get this:
>>> tc()-tc(); tc()-tc()
-2.84952814126882e-005
-1.4247640820030938e-005
which I have not figured out yet)

Terry J. Reedy







More information about the Python-list mailing list