Performance of list comprehensions vs. map

Delaney, Timothy tdelaney at avaya.com
Sun Sep 9 21:02:25 EDT 2001


> From: Chris Barker [mailto:chrishbarker at home.net]
> Tim Peters wrote:
> > > One more question: is it possible for a function to be
> > > mutable??
> > func_code has been writable for some time.  Here under 2.2a3:
> > 
> > >>> def f():
> > ...     pass
> > ...
> > >>> def g():
> > ...     return 666
> > ...
> > >>> f.func_code = g.func_code
> > >>> f()
> > 666
> 
> YOW!
> 
> this really makes optimizing even harder than I thought, and I always
> thought it was hard.

Yep - now you know why so many people have *not* put the effort into
optimising Python. Dynamism pervades almost every part of the language.

The only optimisations I've seen proposed in recent times which truly look
interesting are

PEP 266: Optimizing Global Variable/Attribute Access
http://python.sourceforge.net/peps/pep-0266.html

PEP 267: Optimized Access to Module Namespaces
http://python.sourceforge.net/peps/pep-0267.html

both of which need implementing and testing to determine (a) whether they
are indeed an improvement, and (b) whether they are enough of an improvement
when balanced by how much they complicate the Python core code.

Personally, I'm in favour of PEP 266, which should provide an
across-the-board improvement without overly complicated code (it doesn't
have any special cases, whereas PEP 267 does). However, PEP 267 would do
much of the work as well - both would optimise getting references from other
modules, which currently usually requires looking up the name of the module
(usually local namespace, then module namespace, possibly with __builtin__
namespace) then the attribute in the found module's namespace - i.e. at
least 2 dictionary lookups + (normally) one local lookup. These PEPs would
reduce this to two local lookups - a huge improvement.

Tim Delaney
Cross Avaya R&D
+61 2 9352 9079




More information about the Python-list mailing list