Performance of list comprehensions vs. map

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Wed Sep 5 17:44:45 EDT 2001


Wed, 05 Sep 2001 11:12:44 -0700, Chris Barker <chrishbarker at home.net> pisze:

> I think a big stumbling block is that Python has no concept of the
> "homogenous" sequence.

Do you mean a smart Python compiler/interpreter which tries to optimize
sequence operations in existing programs, on the assumption that many
will be homogeneous?

Or do you mean library support for explicit handling of homogeneous
sequences?


In the first case I think it's very hard, nearly impossible, because
of the mutable nature of Python. For example even if an implementation
knew that all elements of a list are the same class, mapping 2*x+1
through the list without method lookup on each element would need to
ensure that:

- The * operation yields the same class for all arguments, so +
  doesn't need to be looked up in different classes.

- * and + aren't redefined in the class during the loop.


In the second case in alpha versions of Python you can already try
to map seq[0].__class__.__add__ instead of +, but I don't know if it
would be faster. It would check the type instead of method lookup.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list