[Python-ideas] Create a StringBuilder class and use it everywhere

Masklinn masklinn at masklinn.net
Thu Aug 25 12:01:47 CEST 2011


On 2011-08-25, at 11:53 , Carl Matthew Johnson wrote:
> Interesting semantics…
> 
> 
> What version of Python were you using? The current documentation has this to say:
> 
> 	• CPython implementation detail: If s and t are both strings, some Python implementations such as CPython can usually perform an in-place optimization for assignments of the form s = s + t or s += t. When applicable, this optimization makes quadratic run-time much less likely. This optimization is both version and implementation dependent. For performance sensitive code, it is preferable to use thestr.join() method which assures consistent linear concatenation performance across versions and implementations.
> 
> Changed in version 2.4: Formerly, string concatenation never occurred in-place.
> 
> <http://docs.python.org/library/stdtypes.html>
> 
> It's my understanding that the naïve approach should now have performance comparable to the "proper" list append technique as long as you use CPython >2.4.
Hah. Alex Gaynor complained about this very CPython strick recently: http://twitter.com/#!/alex_gaynor/status/104326041920749569


More information about the Python-ideas mailing list