[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())
# my patch
>>> min(mt.Timer("f(42)", ns=dict(f=f)).repeat())

- it is more flexible. Example:
# working code, requires matplotlib
from timeit import Timer
from time import sleep

def linear(i):
def quadratic(i):

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)

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>

More information about the New-bugs-announce mailing list