Re:Efficient String Concatenation in Python
by pjscott on Saturday September 07, 2013 @10:59PM
(#6347618)
I tried it with n=1000 and n=10000, and found that using xrange made it slightly faster, as did using itertools.imap, but the difference was pretty small.
(Here I'm using the stock Python 2.7.1 on a 2011 MacBook Pro.)
Re:Efficient String Concatenation in Python
by pdonis on Sunday September 08, 2013 @12:48PM
(#6349283)
using xrange made it slightly faster
That's what I would expect, since xrange works like a generator; but the internals of its implementation in CPython back when the article ran its original tests were evidently less efficient than they are now.
the difference was pretty small
I suspect that's because the strings being concatenated are really small (the largest will only be 5 bytes for n=10000). I would expect the difference to get bigger as the individual strings get larger.
In Python 3, the map builtin is basically equivalent what itertools.imap was in Python 2, so it is the best choice. (Also, the range builtin in Python 3 no longer realizes a list.)