[BangPypers] which one is the best pythonic way .
Anand Balachandran Pillai
abpillai at gmail.com
Tue Feb 9 09:02:30 CET 2010
On Tue, Feb 9, 2010 at 1:11 PM, Dhananjay Nene <dhananjay.nene at gmail.com>wrote:
> On Tue, Feb 9, 2010 at 1:03 PM, Dhananjay Nene <dhananjay.nene at gmail.com
> > Interestingly in my computations the second approach (zip) is about 35%
> > slower (as measured over 5 runs of 1M iterations each) than the first one
> > (map/lambda). I believe the performance argument should be a bit nuanced.
> > The earlier example you demonstrated with list comprehension is faster
> > to the fact that the if condition (and only the if condition alone -
> > is the equivalent of filter) is inlined vs being a function call. For
> > scenarios not involving a filter, I suspect performance could vary either
> > way. I would rephrase the earlier statement as follows :
It is slower because it makes an inline function call to zip (zip is a
function). If you want your list comprehensions to be as performance
optimized as possible, avoid calling functions in them.
I was using that example to illustrate the apparent "elegance" of the
map solution, not for comparing performances. In this case the map
one could work out to be as good as the list comp one or faster because
of the cost of the function call incurred due to "zip".
In this case, if you want the best performance, just use the itertools
version of zip, i.e izip.
Here are some comparisons.
>>> def f1(): return [(x+y) for x,y in itertools.izip(l1,l2)]
>>> def f2(): return [x+y for x,y in zip(l1,l2)]
>>> def f3(): return map(lambda x,y: x+y, l1,l2)
>>> mytimeit.Timeit(f1, number=10000)
>>> mytimeit.Timeit(f2, number=10000)
>>> mytimeit.Timeit(f3, number=10000)
f1 is the best performer, followed by f2, but f3 is not very bad
either when compared to f2, due to the extra function call cost
incurred in the list comprehension in f2.
> > map, filter, reduce at al are *is** *orders slower when compared to
> > list comprehensions, cuz each invocation of these cost one
> > function call, whereas list comprehensions are optimized in
> > the language.
> I was incorrect above .. there are clearly other situations where the
> benefits of inlining occur apart from the filter function . The statement
> rephrasing is incorrect
> > Regards,
> > ~ Srini T
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.orghttp://mail.python.org/mailman/listinfo/bangpypers
> blog: http://blog.dhananjaynene.com
> twitter: http://twitter.com/dnene http://twitter.com/_pythonic
> BangPypers mailing list
> BangPypers at python.org
More information about the BangPypers