473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Namespaces and the timeit module

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
2 2417
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michele Simionato | last post by:
I use a little wrapper to timeit: $ cat timeit_.py import timeit, __main__, warnings warnings.filterwarnings('ignore', 'import \* only allowed at module level',SyntaxWarning) def...
5
by: ChaosKCW | last post by:
Hi I was wondering if someone could help with the import statements needed to use the timeit module in the following code. I need to access the "cur" object. Thanks, import cx_Oracle...
2
by: Steven D'Aprano | last post by:
When using the timeit module, you pass the code you want to time as strings: import timeit t = timeit.Timer("foo(x, y)", \ """from module import foo x = 27 y = 45 """) elapsed_time =...
2
by: Steven D'Aprano | last post by:
The timeit module is ideal for measuring small code snippets; I want to measure large function objects. Because the timeit module takes the code snippet argument as a string, it is quite handy...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.