On Mon, Aug 1, 2011 at 6:08 AM, Anand Chitipothu <anandol...@gmail.com>wrote:

> > Hang around in #django or #python. The most elegant code that you
> *should*
> > write would invariably be pretty fast (am not ref to asm).
>
> That doesn't mean that any code that is faster is elegant.
>
> IIRC, in python, map function runs slightly faster than list
> comprehensions, but list comprehensions is considered elegant.
>

It is more subtler than that.

List comprehensions are faster than map functions when
the latter needs to invoke a user-defined function call or a lambda.

Maps score over list comprehensions in most cases where the function
is a Python built-in and when no lambda is used.

Example of former:

>>> def f1(): map(sqr, range(1, 100))
...
>>> def f2(): [sqr(x) for x in range(1, 100)]
...
>>> mytimeit.Timeit(f1)
'37.91 usec/pass'
>>> mytimeit.Timeit(f2)
'37.50 usec/pass'

Example of latter:

>>> def f1(): map(hex, range(1, 100))
...
>>> def f2(): [hex(x) for x in range(1, 100)]
...
>>> mytimeit.Timeit(f1)
'49.41 usec/pass'
>>> mytimeit.Timeit(f2)
'55.29 usec/pass'





>
> Anand
> _______________________________________________
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
--Anand
_______________________________________________
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers

Reply via email to