Why it is so dramatical?

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Sep 2 05:57:55 EDT 2002

"Bo M. Maryniuck" <b.maryniuk at forbis.lt> wrote in
news:mailman.1030957743.4021.python-list at python.org: 

> The task:
>      Adding a text to a text.
> The problem:
>      It is tragically slow to add string to huge string.
> The question: Why?

Because the text in strings is stored in a single block of memory, so 
extending a string requires making a copy of all the data. Also strings are 
immutable so you cannot extend them 'in-place'. Like all things it is a 
tradeoff. If you make strings mutable then extending them might be a bit 
faster, but many other operations would then require copying strings in 
case they were modified, so you lose out in general more than you gain.

None of this is specific to Python, the same issues arise in almost any 
programming language (although not always the same solutions).

> The other question: 
>      Is it true, that I should through all the Python life 
>      use lists and then join it into single text instead to use +=
>      operator? 
Using lists and joining them should come to hand as the first solution in 
most cases where you are building up a string.

Using cStringIO, array or mmap should also be in your toolbox for those 
cases where they are the most appropriate.

Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

More information about the Python-list mailing list