Favorite non-python language trick?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Jun 25 21:53:50 EDT 2005


On Sat, 25 Jun 2005 21:30:26 +0200, Peter Otten wrote:

> Mandus wrote:
> 
>> By using the builtin reduce, I
>> move the for-loop into the c-code which performs better.
> 
> No. There is no hope of ever writing fast code when you do not actually
> measure its performance.


Good grief! You've been spying on Mandus! How else could you possibly know
that he doesn't measure performance? Are you running a key-logger on his
machine? *wink*

For the record, perhaps now is a good time to mention that even Guido
recommended the use of map, filter and reduce in some circumstances:

"Try to use map(), filter() or reduce() to replace an explicit for loop,
but only if you can use a built-in function: map with a built-in function
beats for loop, but a for loop with in-line code beats map with a lambda
function!"

http://www.python.org/doc/essays/list2str.html

He specifically mentions that the reason map will often beat a for loop is
that it moves the work out of Python into the underlying C code.

That is also one of the reasons that ''.join(L) is faster than

s = ''
for item in L:
    s = s + item

Do we really need to profile our code every single time to know this?
Isn't it reasonable to just say, "I use join because it is faster than
adding strings" without being abused for invalid optimization?



-- 
Steven.




More information about the Python-list mailing list