News for hackers. Stuff that matters.
Re:Python idioms by kqueue on Sunday June 12, 2011 @01:58PM (#2646967)
~$python -mtimeit "'a' + 'b' + 'c' + 'd'"

10000000 loops, best of 3: 0.026 usec per loop

~$python -mtimeit "''.join(('a','b','c','d'))"

10000000 loops, best of 3: 0.197 usec per loop

Re:Python idioms by Ysx on Sunday June 12, 2011 @02:04PM (#2646990)
Interesting! ''.join() has the advantage on longer strings though:

$ python -mtimeit "'aaaaaaaaaaaaaaa' + 'bbbbbbbbbbbbbbb' + 'ccccccccccccccc' + 'ddddddddddddddd'"

1000000 loops, best of 3: 0.224 usec per loop

$ python -mtimeit "''.join(('aaaaaaaaaaaaaaa','bbbbbbbbbbbbbbb','ccccccccccccccc','ddddddddddddddd'))"

10000000 loops, best of 3: 0.201 usec per loop

Re:Python idioms by kqueue on Sunday June 12, 2011 @03:06PM (#2647132)
That's definitely interesting.
Re:Python idioms by imurray on Sunday June 12, 2011 @02:14PM (#2647015)
Always worth checking. Although the relative merits change when doing many joins to build up a single long string.

This recommendation is to go with .join() by default:

In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations. — http://www.python.org/dev/peps/pep-0008/

despite the fact it might not be better in CPython.

Re:Python idioms by steve-howard on Sunday June 12, 2011 @04:37PM (#2647344)
It's not terribly surprising. Things that scale faster tend to have higher setup costs (cf sorting algorithms; insertion sort is the fastest for relatively small n).
Re:Python idioms by thristian on Sunday June 12, 2011 @11:18PM (#2647988)
I believe the Python runtime has special-case handling for concatenating string-literals, and for concatenating string whose reference count is exactly 1. String concatenation isn't wholly defanged, though.
Re:Python idioms by podperson on Monday June 13, 2011 @09:15AM (#2648890)
You're also constructing the array in the second test.

For a fair test, add elements of an array vs. joining them.