time.clock() problem under linux (precision=0.01s)

Adriaan Renting renting at astron.nl
Fri Aug 19 03:10:20 EDT 2005


One of the limits of at least IBM compatible PC's is that in general they are not more accurate as about 1/64 th of a second if I recall correctly. I think this is the default tick size of the BIOS clock. Next to that the 
BIOS clock itself doesn't need to be very accurate, I can easily drift like an hour a year.
Oh, and on top of that: If you are in a multi taksing operating system this complicates matters even further.

This explains it very well:
http://www.beaglesoft.com/mainfaqclock.htm

Adriaan.
 
>>>"Szabolcs Nagy" <nszabolcs at gmail.com> 08/18/05 1:07 pm >>> 
I have to measure the time of a while loop, but with time.clock i 
always get 0.0s, although python manual sais: 
"this is the function to use for benchmarking Python or timing 
algorithms" 
 
So i tested timer functions capabilities with a short script: 
 
import time 
import os 
 
def test_timer_func(func): 
   print 'min time-time: %.10f'%min(abs(func()-func()) for i in 
xrange(10**5)) 
   print 'max time-time: %.10f'%max(abs(func()-func()) for i in 
xrange(10**5)) 
 
   dt = 0.0 
   loopcount = 0 
   t = func() 
 
   while dt==0.0: 
       dt = func() - t 
       loopcount += 1 
 
   print "min measurable loop time : %.10f"%dt 
   print 'loopcount while dt==0 :',loopcount 
 
 
print '\n time.clock()' 
test_timer_func(time.clock) 
 
print '\n time.time()' 
test_timer_func(time.time) 
 
print '\n os.times()' 
ot = os.times 
test_timer_func(lambda:ot()[4]) 
 
 
My output is: 
 
time.clock() 
min time-time: 0.0000000000 
max time-time: 0.0100000000 
min measurable loop time : 0.0100000000 
loopcount while dt==0 : 2703 
 
time.time() 
min time-time: 0.0000019073 
max time-time: 0.0000460148 
min measurable loop time : 0.0000050068 
loopcount while dt==0 : 1 
 
os.times() 
min time-time: 0.0000000000 
max time-time: 0.0100000007 
min measurable loop time : 0.0099999998 
loopcount while dt==0 : 2515 
 
 
So the precision of time.clock is 0.01s under my ubuntu linux system, 
which means it's not suitable for benchmarking. (i want to benchmark 
the fps in my pygame+pyode program and it needs at least 0.001s 
precision) 
 
time.time seems much better solution, but python manual sais: "not all 
systems provide time with a better precision than 1 second" 
 
Should i use time.clock or time.time to be more crossplatform? 
Is time.time ok for windows? (time()-time() != 0.0) 
 
nszabolcs 
 
-- 
http://mail.python.org/mailman/listinfo/python-list 




More information about the Python-list mailing list