Fastest technique for string concatenation

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Oct 2 22:50:43 EDT 2010


On Sat, 02 Oct 2010 13:17:02 -0700, Carey Tilden wrote:

> Have you profiled an application and found string concatenation to be
> a performance bottleneck?  I would be surprised, but it's always
> possible.  If not, I'd suggest you choose the technique that is most
> clear and concise, and worry about performance only if it becomes a
> real issue.

While I agree with your general point about premature optimization, I 
would not be the tiniest bit surprised to discover that string 
concatenation was a performance bottleneck -- and that it only shows up 
under some platforms and not others!

Repeated string concatenation risks being an O(n**2) algorithm, which 
performs terribly. Last year there was a bug report of awful performance 
in the httplib module that only effected Windows users:

http://www.mail-archive.com/python-dev%40python.org/msg40692.html

httplib was FOUR HUNDRED times slower than IE at copying a file over a 
local network. Eventually the fault was tracked down to repeated string 
concatenation in the module:

http://www.mail-archive.com/python-dev%40python.org/msg41150.html

It costs nothing to avoid repeated string concatenation even prior to 
demonstrating a problem.



-- 
Steven



More information about the Python-list mailing list