-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass a namespace to timeit #46779
Comments
I'd like to suggest a different approach than the one taken in rev. - It has smaller overhead for functions that take an argument:
>>> def f(a): pass
...
# trunk
>>> min(ht.Timer(lambda f=f: f(42)).repeat())
0.54068493843078613
# my patch
>>> min(mt.Timer("f(42)", ns=dict(f=f)).repeat())
0.29009604454040527
def linear(i):
sleep(.05*i)
def quadratic(i):
sleep(.01*i**2)
x = range(10)
y = []
for a in x:
y.append([min(Timer("f(a)", ns=dict(f=f, a=a)).repeat(1, 1))
for f in linear, quadratic])
from pylab import plot, show
plot(x, y)
show() The above code works unaltered inside a function, unlike the hacks
The provided patch is against 2.5.1. If it has a chance of being |
A more general approach would be to add both 'locals' and 'globals' to |
On the second thought, I actually wanted Timer to mimic eval without |
Generally, when I use timeit from the interpreter prompt, I use "from |
Alexander, I'm fine with a more specific argument name. ns was what Antoine, from __main__ import name1, ..., nameN works fine on the |
On Wed, Apr 2, 2008 at 2:42 AM, Peter Otten <report@bugs.python.org> wrote:
Maybe it should be "locals" after all. It does not look like the |
This note is simply a reminder that Antoine's 'from __main__ import *' >>> import timeit
>>> timeit.timeit('None','from __main__ import *')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.0/timeit.py", line 227, in timeit
return Timer(stmt, setup, timer).timeit(number)
File "/usr/local/lib/python3.0/timeit.py", line 135, in __init__
code = compile(src, dummy_src_name, "exec")
File "<timeit-src>", line 2
SyntaxError: import * only allowed at module level |
Georg, why did you reassign this? |
I'm sorry, this should have been another issue. Reassigning to you. |
See related discussion in bpo-5441 and bpo-1397474. |
Would still be nice to have something like this. The timeit module API is still crippled, especially now that "from __main__ import *" doesn't work in a function anymore. |
Attached is a patch that adds a 'global' kwarg to the Timeit constructor, which does pretty much what it says on the tin: specifies a global namespace that exec() will use. I originally had a 'locals' arg as well (to mirror the signature of eval/exec), but realized that the local vars I was passing to exec were not available to the inner function. Reason: the timeit module compiles/execs a *closure*, and closed-over variables and exec() simply do not play nicely. Possible workarounds were to munge locals() into the globals() dict, or to somehow inject the variables in the locals dict into the closure. I found neither of these options superior to simply not including a locals argument, for reasons of Least Surprise and maintainability. Patch includes some basic tests and documentation. I am particularly uncomfortable with writing docs so those very likely need some polish. |
Correction, the name of the argument is 'globals', not 'global'. |
Ben, thanks for the patch. Have you signed a contributor's agreement? You can find it at https://www.python.org/psf/contrib/contrib-form/ |
I did sign one right after I submitted the patch. Takes a few days for the asterisks to propagate I guess :) |
Ah, good. The patch looks fine to me, except that you should add "versionchanged" tags in the documentation for the added parameter. |
Ah yes. New patch improves the docs. |
New changeset e0f681f4ade3 by Antoine Pitrou in branch 'default': |
Thank you, Ben! Your patch is now pushed to the default branch. |
Thanks Antoine. Cheers :-) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: