Performance of list comprehensions vs. map

Paul Winkler slinkp23 at yahoo.com
Fri Sep 7 16:17:12 EDT 2001


On Thu, 6 Sep 2001 13:38:07 +0200, Alex Martelli <aleax at aleax.it> wrote

(snip - a very long and detailed discussion of map, list
comprehensions, and for/append loops)

Thanks very much, Alex, now I understand what's happening under the
covers! I guess that optimizing list comprehensions in this way is
probably not worth the trouble, given the factors you mention.  The
optional "if" clause of list comprehensions also complicates things.

I tried your test script, and got comparable results to yours, with
the exception that on my machine, use_map always beats pre_alloc even
when len() is "wrong" (although not by much). I tried it with
n=1000*1000 and n = 100 * 100 and got similarly proportioned results.

So for the foreseeable future, looks like map() is likely to remain
the fastest way to generate lists.

-- Paul Winkler




More information about the Python-list mailing list