Easy questions from a python beginner

Peter Otten __peter__ at web.de
Fri Jul 23 07:20:19 EDT 2010


wheres pythonmonks wrote:

> Funny... just spent some time with timeit:
> 
> I wonder why I am passing in strings if the callback overhead is so
> light...
> 
> More funny:  it looks like inline (not passed in) lambdas can cause
> python to be more efficient!
>>>> import random
>>>> d = [ (['A','B'][random.randint(0,1)],x,random.gauss(0,1)) for x in
>>>> xrange(0,1000000) ] def A1(): j = [ lambda t: (t[2]*t[1],t[2]**2+5) for
>>>> t in d ]
> 
>>>> def A2(): j = [ (t[2]*t[1],t[2]**2+5) for t in d ]
> 
>>>> def A3(l): j = [ l(t) for t in d]
> 
>>>> import timeit
>>>> timeit.timeit('A1()','from __main__ import A1,d',number=10);
> 2.2185971572472454
>>>> timeit.timeit('A2()','from __main__ import A2,d',number=10);
> 7.2615454749912942
>>>> timeit.timeit('A3(lambda t: (t[2]*t[1],t[2]**2+5))','from __main__
>>>> import A3,d',number=10);
> 9.4334241349350947
> 
> So: in-line lambda possible speed improvement.  in-line tuple is slow,
> passed-in callback, slowest yet?
> 
> Is this possibly right?
> 
> Hopefully someone can spot the bug?

A1() makes a lot of lambdas but doesn't invoke them. Once that is fixed A1() 
is indeed slower than A2():

>>> from timeit import timeit
>>> import random
>>> d = [(random.choice("AB"), x, random.gauss(0, 1)) for x in 
xrange(10**6)]
>>> def A1(d=d): return [(lambda t: (t[2]*t[1],t[2]**2+5))(t) for t in d]
...                                                                      
>>> def A2(d=d): return [(t[2]*t[1], t[2]**2+5) for t in d]               
...                                                                       
>>> assert A1() == A2()
>>> timeit("A1()", "from __main__ import A1", number=10)
14.275790929794312
>>> timeit("A2()", "from __main__ import A2", number=10)
10.31659197807312

Peter



More information about the Python-list mailing list