[New-bugs-announce] [issue2527] Pass a namespace to timeit
Peter Otten
report at bugs.python.org
Tue Apr 1 14:13:47 CEST 2008
New submission from Peter Otten <__peter__ at web.de>:
I'd like to suggest a different approach than the one taken in rev.
54348 to improve timeit's scripting interface: allow passing it a
namespace. Reasons:
- 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
- it is more flexible. Example:
# working code, requires matplotlib
from timeit import Timer
from time import sleep
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
using "from __main__ import ...".
- the implementation is simpler and should be easy to maintain.
The provided patch is against 2.5.1. If it has a chance of being
accepted I'm willing to jump through the necessary hoops:
documentation, tests, etc.
----------
components: Library (Lib)
files: diff_against_2_5_1.txt
messages: 64805
nosy: potten
severity: normal
status: open
title: Pass a namespace to timeit
type: feature request
versions: Python 2.6
Added file: http://bugs.python.org/file9916/diff_against_2_5_1.txt
__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2527>
__________________________________
More information about the New-bugs-announce
mailing list