Namespaces and the timeit module
Roy Smith
roy at panix.com
Mon Dec 13 20:58:48 EST 2004
I'm playing with the timeit module, and can't figure out how to time a
function call. I tried:
def foo ():
x = 4
return x
t = timeit.Timer ("foo()")
print t.timeit()
and quickly figured out that the environment the timed code runs under
is not what I expected:
Traceback (most recent call last):
File "./d.py", line 10, in ?
print t.timeit()
File "/usr/local/lib/python2.3/timeit.py", line 158, in timeit
return self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
NameError: global name 'foo' is not defined
In fact, trying to time "print dir()" gets you:
['_i', '_it', '_t0', '_timer']
It seems kind of surprising that I can't time functions. Am I just not
seeing something obvious?
More information about the Python-list
mailing list