String concatenation vs. string formatting

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jul 9 01:30:55 EDT 2011


Billy Mays wrote:

> If it means anything, I think concatenation is faster.

You are measuring the speed of an implementation-specific optimization.
You'll likely get *very* different results with Jython or IronPython, or
old versions of CPython, or even if you use instance attributes instead of
local variables.

It also doesn't generalise: only appends are optimized, not prepends.

Worse, the optimization can be defeated by accidents of your operating
system's memory management, so code that runs snappily and fast on one
machine will run SLLLOOOOOOWWWWWWWWWWWWWWWLY on another.

This is not a hypothetical risk. People have been burned by this in real
life:

http://www.gossamer-threads.com/lists/python/dev/771948

If you're interested in learning about the optimization:

http://utcc.utoronto.ca/~cks/space/blog/python/ExaminingStringConcatOpt




-- 
Steven




More information about the Python-list mailing list