Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Simplified timeit (http://www.velocityreviews.com/forums/t332282-simplified-timeit.html)

Connelly Barnes 06-18-2004 05:31 AM

Simplified timeit
 
I've been avoiding the timeit module because it seems
overly complicated. I finally coded my own function,
which has the following benefits:

1. Simple
2. Figures out how many times to evaluate the
callable passed to it.

Here's the code:

def pytime(f, *args, **kwargs):
"""Calls f many times to determine the average time
to execute f. Usually accurate to within 3%."""
from time import clock
count = 1
while True:
start = clock()
for i in range(count): f(*args, **kwargs)
end = clock()
T = end - start
if T >= 0.5: break
count *= 2
return T / count

Example:

from math import *

def f(x): return x * x - cos(x)

t = pytime(f, 1.0)
print 'Time to execute f: ', t
print 'Calls to f per second: ', 1.0/t

PS: Try repeating this benchmark with psyco. I get a
70x increase in speed.

Connelly Barnes


All times are GMT. The time now is 07:57 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.