String concatenation performance with +=

Steven D'Aprano steve at pearwood.info
Sat Feb 14 01:08:58 EST 2009


Steven D'Aprano wrote:

> Benjamin Peterson wrote:
> 
>> Sammo <sammo2828 <at> gmail.com> writes:
>> 
>>> String concatenation has been optimized since 2.3, so using += should
>>> be fairly fast.
>> 
>> This is implementation dependent and shouldn't be relied upon.
> 
> It's also a fairly simple optimization and really only applies to direct
> object access, not items or attributes.
> 
>>>> Timer('s += "x"', 's = ""').repeat(number=100000)
> [0.067316055297851562, 0.063985109329223633, 0.066659212112426758]

Oops, sorry, I should have said... Timer is timeit.Timer.


-- 
Steven




More information about the Python-list mailing list