Performance of list comprehensions vs. map

Paul Winkler slinkp23 at yahoo.com
Wed Sep 5 20:50:17 CEST 2001


On Wed, 05 Sep 2001 11:12:44 -0700, Chris Barker <chrishbarker at home.net> wrote:
>Hi all,
>
>I just took another look at:
>
>http://musi-cal.mojam.com/~skip/python/fastpython.html
>
>specifically, the loops section:
>
>"""
>Loops

(SNIP)
>List comprehensions were added to Python in version 2.0 as well. They
>provide a syntactically more compact way of writing the above for loop: 
>
>newlist = [s.upper() for s in list]
>
>It's not really any faster than the for loop version, however. 
>"""

Hmm... I just played around a bit more and it is in fact a bit faster,
but not much; as the size of the list grows, the time to do a list
comprehension vs. the time to do a for loop seems to converge, while
the time to do it via map is always fastest, sometimes by a lot.

>my question is: Why not? I like list comprehensions a lot, and they
>would seem to offer a way to get optimization over a for loop, much like
>map does, but apparently not. If map can do the loop in C, why can't a
>list comprehension?

That's what I want to know, too!

(SNIP description of homogenous lists)

>I've proposed similar ideas in the past, and not gotten much response. I
>imagine my ideas are full of holes, but no one has taken the time to
>point them out to me yet. I have come to the conclusion that the really
>sharp minds on this list (and the folks that might be qualified to
>actually impliment such a thing) are not really interested in something
>like this that is a perfomance only improvement. Am I right? or is the
>idea just so stupid that it's not worth commenting on?

Well, I don't think I qualify as among the really sharp minds here,
but I think it's a great idea.

--PW






More information about the Python-list mailing list