473,786 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2113
<ru***@yahoo.co m> 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.co m> 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***********@a cm.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***********@a cm.org
Dec 6 '05 #6

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

Similar topics

3
2083
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", "copyright", "credits" or "license" for more information. >>> import timeit >>> import time >>> t=timeit.Timer("pass","pass",time.time) >>> print t.timeit()
2
2466
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
4092
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 /usr/lib/python2.3/timeit.py But if I specify the full path, it works:
2
2701
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 = t.timeit()
2
1771
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 pasted) with the same results (Win2k, Python 2.4). Any help is appreciated. The output follows: #1 C:\Python24\Lib>timeit.py 'if hasattr(int, "__nonzero__"): pass' Traceback (most recent call last): File "C:\Python24\Lib\timeit.py", line 285, in ?...
2
3307
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 to use from the command line, but it is less convenient for timing large pieces of code or when working in the interactive interpreter. E.g. variations on this *don't* work: $ python Python 2.4.3 (#1, Jun 13 2006, 11:46:08)
3
5528
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 = timeit.Timer("callFunc(line, i)","from __main__
7
6424
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 in a class? class FetchUrlThread(threading.Thread):
7
1997
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
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.