String concatenation performance with +=

Benjamin Peterson benjamin at python.org
Fri Feb 13 23:12:29 EST 2009


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.

> 
> Note that I need to do something to mydata INSIDE the loop, so please
> don't tell me to append moredata to a list and then use "".join after
> the loop.

Then why not just mutate the list and then call "".join?

> 
> 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.







More information about the Python-list mailing list