473,412 Members | 2,012 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,412 software developers and data experts.

timeit's environment

Why doesn't the following work? It generates a "NameError: global
name 'data' is not defined" error.

import timeit

global data
data = [3,8,4,8,6,0,5,7,2,1]

env = "global data; x = data"

print timeit.Timer('x.sort()', env).timeit()
print timeit.Timer('x.sort(cmp=cmp', env).timeit()

How can I get timeit() to see an external (to it) variable?
(In the real program 'data' is very expensive to create and
contains non-reproducable data and the two timeit calls
must be run on identical objects.

Dec 5 '05 #1
5 2101
<ru***@yahoo.com> wrote:
Why doesn't the following work? It generates a "NameError: global
name 'data' is not defined" error.

import timeit

global data
data = [3,8,4,8,6,0,5,7,2,1]

env = "global data; x = data"

print timeit.Timer('x.sort()', env).timeit()
print timeit.Timer('x.sort(cmp=cmp', env).timeit()

How can I get timeit() to see an external (to it) variable?
(In the real program 'data' is very expensive to create and
contains non-reproducable data and the two timeit calls
must be run on identical objects.


You have to use 'from __main__ import data as x' rather than just say
'global data; x=data', because timeit is a separate module from your
__main__ one.
Alex
Dec 5 '05 #2

Alex Martelli wrote:
<ru***@yahoo.com> wrote:
Why doesn't the following work? It generates a "NameError: global
name 'data' is not defined" error.

import timeit

global data
data = [3,8,4,8,6,0,5,7,2,1]

env = "global data; x = data"

print timeit.Timer('x.sort()', env).timeit()
print timeit.Timer('x.sort(cmp=cmp', env).timeit()

How can I get timeit() to see an external (to it) variable?
(In the real program 'data' is very expensive to create and
contains non-reproducable data and the two timeit calls
must be run on identical objects.


You have to use 'from __main__ import data as x' rather than just say
'global data; x=data', because timeit is a separate module from your
__main__ one.


Ahh, (slaps forehead) that makes sense. Thank you.

After posting I looked again at the documentation and at the
bottom of the example subsection, they also mention using
import (although without explaining why.)

Since I've been bitching about documentation in another
thread, I'm curious... Would it be obvious to anyone of
low to intermediate python skills that using global would
not work in this case? Would it be obvious that using an
import is the answer? Or can I blame this partially on the
documentation? :-) I think the scoping issue could have
at least been mentioned in the Timer class or timeit method
descriptions. There is no mention there of exactly what
environment the code is run in.

Dec 5 '05 #3
ru***@yahoo.com wrote:
Since I've been bitching about documentation in another
thread, I'm curious... Would it be obvious to anyone of
low to intermediate python skills that using global would
not work in this case? Would it be obvious that using an
import is the answer? Or can I blame this partially on the
documentation? :-) I think the scoping issue could have
at least been mentioned in the Timer class or timeit method
descriptions. There is no mention there of exactly what
environment the code is run in.


Perhaps you could write a paragraph or two that would have
informed you and send it to the destination mentioned on the
documentation page.
--
-Scott David Daniels
sc***********@acm.org
Dec 5 '05 #4

Scott David Daniels wrote:
ru***@yahoo.com wrote:
Since I've been bitching about documentation in another
thread, I'm curious... Would it be obvious to anyone of
low to intermediate python skills that using global would
not work in this case? Would it be obvious that using an
import is the answer? Or can I blame this partially on the
documentation? :-) I think the scoping issue could have
at least been mentioned in the Timer class or timeit method
descriptions. There is no mention there of exactly what
environment the code is run in.


Perhaps you could write a paragraph or two that would have
informed you and send it to the destination mentioned on the
documentation page.


Yes, but I was hoping to get some sense of how such
a submission might be viewed prior to going through
the work of doing it.

Dec 5 '05 #5
ru***@yahoo.com wrote:
Scott David Daniels wrote:

....
Perhaps you could write a paragraph or two that would have
informed you and send it to the destination mentioned on the
documentation page.


Yes, but I was hoping to get some sense of how such
a submission might be viewed prior to going through
the work of doing it.

I assure you that if it does seem to ease the pain we'd love to
include it. A newbie knows what's confusing about the intro parts.
Once you've been around a while, you no longer even read those
intro things, and so don't know where they suffer. "It's to hard
for me to understand" is a content-free complaint that is too often
heard, but "if you said, 'In this instance be sure to ...' in paragraph
three ..." is a clear explanation of what is wrong and how it might be
fixed that will definitely be read.

--
-Scott David Daniels
sc***********@acm.org
Dec 6 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dan Christensen | last post by:
The test below is done with Python 2.4a1 compiled from source, but the same thing happens with Debian's python2.3_2.3.4-2. Python 2.4a1 (#1, Jul 11 2004, 12:20:32) on linux2 Type "help",...
2
by: Roy Smith | last post by:
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()
2
by: Aggelos I. Orfanakos | last post by:
Hello. Under Gentoo Linux, I issue: $ python timeit.py python: can't open file 'timeit.py' $ ls -al /usr/lib/python2.3/timeit.py -rw-r--r-- 1 root root 9833 Oct 19 02:17...
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: 3c273 | last post by:
Hello, I was reading the thread on try/except overhead and decided to try Alex's examples but they kept generating exceptions. So I went to the docs and tried the examples there (copied and...
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...
3
by: silverburgh.meryl | last post by:
Hi, I have a function in my python like this: def callFunc(line, no): # some code And I want to do a performance test like this: for line in f: for i in range(int(count)): t1 =...
7
by: silverburgh.meryl | last post by:
Hi, I am using timeit to time a global function like this t = timeit.Timer("timeTest()","from __main__ import timeTest") result = t.timeit(); But how can i use timeit to time a function...
7
by: ssecorp | last post by:
I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.