On 22.07.2012 20:01, Steven D'Aprano wrote:
[SNIP]
map is faster than an ordinary for-loop if the function you are applying
is a builtin like int, str, etc. But if you have to write your own pure-
Python function, the overhead of calling a function negates the advantage
of map, which is no faster than a for-loop. For example:
results = map(int, sequence) # calls builtin `int`
hoists the call to int into the fast C layer, instead of the slow Python
layer, and should be faster than
results = []
for x in sequence:
results.append(int(x))
which runs at the speed of Python. But:
results = map(lambda x: x+1, sequence) # calls pure Python function
if no faster than a for-loop:
results = []
for x in sequence:
results.append(x+1)
Note: this has*nothing* to do with the use of lambda. Writing the "+1"
function above using def instead of lambda would give the same results.
[SNAP]
Hi Steven,
besides that I testdrive Pypy (and still am impressed, other topic) -
your answer was what I was picking for ;)
Especially this part of you:
> map is faster than an ordinary for-loop if the function you are applying
> is a builtin like int, str, etc. [underlaying c-layer] But if you
have to write your own pure-
> Python function, the overhead of calling a function negates the advantage
> of map [...]
I did not know that the speed gain is up foremost present when using
built-ins, but that's for sure something to keep in mind when writing code.
Thanks for your explanation, clarifies a lot!
Jan
--
http://mail.python.org/mailman/listinfo/python-list