469,282 Members | 2,359 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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

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

Aug 18 '05 #1
3 2910
On 18 Aug 2005 04:07:18 -0700, "Szabolcs Nagy" <ns*******@gmail.com>
declaimed the following in comp.lang.python:

Should i use time.clock or time.time to be more crossplatform?
Is time.time ok for windows? (time()-time() != 0.0)
PythonWin 2.3.5 (#62, Feb 9 2005, 16:17:08) [MSC v.1200 32 bit (Intel)]
on win32.
Portions Copyright 1994-2001 Mark Hammond (mh******@skippinet.com.au) -
see 'Help/About PythonWin' for further copyright information.
import time
time.time() - time.time() 0.0 time.clock() - time.clock() -1.2882053580379413e-006

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Aug 18 '05 #2
"Szabolcs Nagy" <ns*******@gmail.com> writes:
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)


You should use the timeit module, which chooses the correct timer to
use for the platform it's running on, as well as working around a
number of other things that trip up people trying to benchmark code.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Aug 19 '05 #3
ncf
Woa, if you don't mind my asking, why do you do a time-cache on your
messages?

Aug 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by bart_nessux | last post: by
8 posts views Thread by peterbe | last post: by
3 posts views Thread by Russell Warren | last post: by
20 posts views Thread by Jean Johnson | last post: by
5 posts views Thread by Tobiah | last post: by
5 posts views Thread by yinglcs | last post: by
37 posts views Thread by David T. Ashley | last post: by
12 posts views Thread by pekka | last post: by
8 posts views Thread by Theo v. Werkhoven | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.