March 20, 2005
8:09 p.m.
[Nick Coghlan]
So, using "".join is roughly three times as fast as abusing sum :)
True in the case where you're concatenating three strings, but what about 100 strings? (Full source attached) Py> timeit.Timer("sum(strings, ai)", setup).repeat() [33.553668413164239, 32.861660909417253, 33.092705357803851] Py> timeit.Timer("''.join(strings)", setup).repeat() [5.4385152302876492, 5.3633637794747671, 5.3587657090496066] Py> timeit.Timer(" + ".join('"' + s + '"' for s in strings), "").repeat() [17.726616371633828, 17.785511845779279, 18.179861127601413] So at 100 strings, the difference is over 5x, and I assume you'll see the relative distance increase as you increase the number of strings. Timothy Fitz