map versus the 'for' loop

Tim Peters tim_one at email.msn.com
Sat Dec 4 22:29:22 EST 1999


[Tim]
> If func is None, map is much faster....
> If func is some builtin operation (like "+" or "len") spelled at least
> roughly the same way in both cases, ... map will usually be
> significantly faster ...

[Will Ware]
> Pardon my density: I can easily see how to do this with "len", since
> I can type "len" at the prompt and get a "<built-in function len>"
> object back, but I can't see how to do that with "+". The best I've
> been able to come up with is the lambda that you later say isn't a
> good idea.

A lambda is much slower, but I didn't say that's bad -- it is bad, but for
other reasons <wink>.

> How do I obtain a "<built-in function +>" object?

>>> import operator
>>> operator.add
<built-in function add>
>>>

Curiously, this one illustrates the "it depends on the precise operation"
clause in the original reply.  For obscure implementation reasons,
operator.add isn't as fast as infix "+" when applied to ints, but
operator.div is about as fast as infix "/".

worry-about-speed-and-you'll-never-sleep-again-ly y'rs  - tim






More information about the Python-list mailing list