[Python-Dev] accumulator display syntax (original) (raw)

Alex Martelli aleaxit at yahoo.com
Tue Oct 21 17:16:22 EDT 2003


On Tuesday 21 October 2003 10:46 pm, Guido van Rossum wrote:

y = 1 sum([x*y for x in R])

fares compared to y = 1 def gen(): for x in R: yield y*y sum(gen())

module a.py being:

R = [range(N) for N in (10, 100, 10000)]

def lc(R): y = 1 sum([x*y for x in R])

def gen1(R): y = 1 def gen(): for x in R: yield y*y sum(gen())

def gen2(R): y = 1 def gen(R=R, y=y): for x in R: yield y*y sum(gen())

i measure:

for N=10: [alex at lancelot bo]$ timeit.py -c -s'import a' 'a.lc(a.R[0])' 100000 loops, best of 3: 12.3 usec per loop [alex at lancelot bo]$ timeit.py -c -s'import a' 'a.gen1(a.R[0])' 100000 loops, best of 3: 10.4 usec per loop [alex at lancelot bo]$ timeit.py -c -s'import a' 'a.gen2(a.R[0])' 100000 loops, best of 3: 9.7 usec per loop

for N=100: [alex at lancelot bo]$ timeit.py -c -s'import a' 'a.lc(a.R[1])' 10000 loops, best of 3: 93 usec per loop [alex at lancelot bo]$ timeit.py -c -s'import a' 'a.gen1(a.R[1])' 10000 loops, best of 3: 59 usec per loop [alex at lancelot bo]$ timeit.py -c -s'import a' 'a.gen2(a.R[1])' 10000 loops, best of 3: 55 usec per loop

for N=10000: [alex at lancelot bo]$ timeit.py -c -s'import a' 'a.lc(a.R[2])' 100 loops, best of 3: 9.4e+03 usec per loop [alex at lancelot bo]$ timeit.py -c -s'import a' 'a.gen1(a.R[2])' 100 loops, best of 3: 5.6e+03 usec per loop [alex at lancelot bo]$ timeit.py -c -s'import a' 'a.gen2(a.R[2])' 100 loops, best of 3: 5.2e+03 usec per loop

I think it's well worth overcoming come "community resistance to new syntax" to get this kind of advantage easily. The trick of binding outer-scope variables as default args is neat but buys less than the pure idea of just using a generator rather than a list comprehension.

Alex



More information about the Python-Dev mailing list