StringType add operation seems every inefficient

Janko Hauser jhauser at ifm.uni-kiel.de
Wed Feb 14 08:46:11 EST 2001


"Lee, Rick" <rickylee at americasm01.nt.com> writes:

> If I do something like this:
> 
> s = ''
> for i in listOfStrings:
>     s = s + i
> 
This always generates a new string object, so there is a lot of memory
copies involved.

> Once listOfStrings is say 10000 members of 100 bytes, it takes forever
> (ie. minutes) for the "for" loop to finish.  This is running on a 700MHz
> WinNT machine, Python 2.0
> 
> Contrast with:
> 
> s = ''
> blocks = []
> for i in listOfStrings:
>     s = s + i
>     if len(s) > 4000
>         blocks.append(s); s = ''
> 
if you have the string parts already in a list and then do a
string.join(listOfStrings) this should be much faster. (In Python2.0 this
should be ''.join(list))

HTH,
__Janko

-- 
  Institut fuer Meereskunde             phone: 49-431-597 3989
  Dept. Theoretical Oceanography        fax  : 49-431-565876
  Duesternbrooker Weg 20                email: jhauser at ifm.uni-kiel.de
  24105 Kiel, Germany



More information about the Python-list mailing list