[BangPypers] which one is the best pythonic way .

Dhananjay Nene dhananjay.nene at gmail.com
Tue Feb 9 08:41:51 CET 2010


On Tue, Feb 9, 2010 at 1:03 PM, Dhananjay Nene <dhananjay.nene at gmail.com>wrote:

>  Anand Balachandran Pillai wrote:
>
> On Tue, Feb 9, 2010 at 10:52 AM, Srinivas Reddy Thatiparthy <srinivas_thatiparthy at akebonosoft.com> wrote:
>
>
>
>  Thanks for the replies and I avoid using lambdas..
> Btw,Shall I avoid using filter and map ?
> Because what ever filter and map do,I could seem to do the same with
> Listcomprehensions..
> Is there any situation in which they fare better than list
> comprehensions?
>
>
>
>     map, filter, reduce at al are orders slower when compared to
>    list comprehensions, cuz each invocation of these cost one
>    function call, whereas list comprehensions are optimized in
>    the language.
>
>   For example,
>
>  >>> def f1(): [i for i in range(1000) if i % 3 == 0]
> ...
>  >>> def f2(): filter(lambda x: x%3==0, range(1000))
>
>  And using timeit,
>
>
>
>   mytimeit.Timeit(f1, number=10000)
>
>
>   '211.47 usec/pass'
>
>
>   mytimeit.Timeit(f2, number=10000)
>
>
>   '319.08 usec/pass'
>
> The only advantage of map, filter etc over list comprehensions is
> that they are sometimes concise over the equivalent list
> comprehensions, when combined with already defined functions.
>
> For example,
>
>
>
>   [ x for x in range(1000) if (x%3==0) or (x%5==0)]
> def f(x): (x%3==) or (y %5==0)
> map(f, range(1000))
>
>
>   But again, keep in mind that these are much slower when compared
> to the list comprehension or generator equivalent.
>
> There are a few cases when a map function looks more "elegant"
> when compared to the equivalent list comp.
>
> For example, let us say you want to add together two lists.
>
>
>
>   l1=range(5, 10)
> l2=range(10, 15)
>
>
>       map(lambda x,y: x+y, l1, l2)
>
>
>   [15, 17, 19, 21, 23]
>
> There is no direct way to do this in list comp except taking
> the help of "zip".
>
>
>
>   [a+b for a,b in zip(l1,l2)]
>
>
>   [15, 17, 19, 21, 23]
>
> But practicality beats purity. Given a choice, I will always use the
> latter instead of the former, irrespective of the "elegance", which
> is questionable and subjective anyway :)
>
>
>  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 due
> to the fact that the if condition (and only the if condition alone - which
> is the equivalent of filter) is inlined vs being a function call. For other
> scenarios not involving a filter, I suspect performance could vary either
> way. I would rephrase the earlier statement as follows :
>
> 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


More information about the BangPypers mailing list