String concatenation performance with +=

Sammo sammo2828 at gmail.com
Sat Feb 14 13:00:39 CET 2009


On Feb 14, 4:47 pm, Steven D'Aprano <st... at pearwood.info> 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.
>
> >> Why is the second test so much slower?
>
> > Probably several reasons:
>
> > 1. Function call overhead is quite large compared to these simple
> > operations. 2. You are resolving attribute names.
>
> 3. Because the optimization isn't applied in this case.

Thanks Steven -- that's the answer I was looking for. The += string
concatenation optimization only applies to local variables. In my
case, I incorrectly assumed it applied to attributes.

Can you point me to any references regarding the limitations of the
optimization? I guess it's another Python idiom that's documented
somewhere ...



More information about the Python-list mailing list