Speed of string += string
Alex Martelli
aleax at aleax.it
Sat Apr 12 18:41:42 EDT 2003
Mads Orbesen Troest wrote:
> On Sat, 12 Apr 2003 18:45:26 GMT, Alex Martelli wrote:
>
>> Building up lists of strings with mylist.append(piece), then joining
>> them together at the end with ''.join(mylist), is the canonical solution;
>> others, which I've seen already pointed out to you, include using
>> cStringIO.
>
> Alex, thanks for your response. I had a hunch the += approach would be a
> performance killer. I'll try the list.append approach.
You're welcome, and my compliments for your accurate hunch -- to most
people, this particular trap comes as a surprise (it did to me the
first time I met it [which wasn't in Python, but in a C++ library
that didn't meet std::vector::push_back performance guarantees...]),
though I think it becomes quite clear as soon as it's explained.
Alex
More information about the Python-list
mailing list