Re:Python Patterns - An Optimization Anecdote
by teoruiz on Tuesday November 02, 2010 @08:45AM
(#1860179)
It is actually slower. Adding an f8 function using the ''.join() method:
Original code linked in the article: http://www.python.org/doc/essays/f.py
[ Parent
| on HN ]
Re:Python Patterns - An Optimization Anecdote
by scott_s on Tuesday November 02, 2010 @11:20AM
(#1860764)
My results agree with yours, in terms of who wins. Python 2.6 (2.4 is similar, and 3.0 is as well, for the techniques that were still valid) on OSX 10.5, Intel Core Duo at 2 GHz:
[scotts@silver]$ /opt/local/bin/python2.6 f.py
f1 0.107
f2 0.19
f3 0.106
f4 0.097
f5 0.139
f6 0.073
f7 0.025
f8 0.103
edit: I added a ninth function, def f9(list):
return ''.join(map(chr, list))
To test if the difference between f8 and f6 was the list comprehension or the joinfields method. A result of 0.072 tells me it's the list comprehension. The only difference between f8 and f9 is list comprehension versus map. That leads me to conclude that map is indeed a faster way to construct a list than a list comprehension. (Although this may change for a user-defined function. I could test this, but I've spent enough time already.)