By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,728 Members | 1,355 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,728 IT Pros & Developers. It's quick & easy.

Namespaces and the timeit module

P: n/a
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?
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Roy Smith <ro*@panix.com> writes:
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?


Like the documentation for Timer? :-)

class Timer([stmt='pass' [, setup='pass' [, timer=<timer function>]]])

You can't use statements defined elsewhere, you have to define them in
the setup arguments (as a string). Like this:
define_foo = '''
def foo():
x = 4
return x
'''

t = timeit.Timer("foo()" setup=define_foo)
print t.timeit()
One common idiom I've seen is to put your definition of foo() in a
module (say x.py), then, from the command line:

$ python -m timeit -s 'from x import foo' 'foo()'

(the -m is for python 2.4 to run the timeit module; use the full path
to timeit.py instead for earlier pythons)

Alternatively, the examples for the timeit module has another way to
time functions defined in a module.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #2

P: n/a
co**********@physics.mcmaster.ca (David M. Cooke) wrote:
It seems kind of surprising that I can't time functions. Am I just not
seeing something obvious?


Like the documentation for Timer? :-)

class Timer([stmt='pass' [, setup='pass' [, timer=<timer function>]]])

You can't use statements defined elsewhere, you have to define them in
the setup arguments (as a string).


Doh! Of course. Now that you point it out, it makes perfect sense, but
I didn't get that from reading the description. Thanks.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.