On Sat, Mar 22, 2008 at 09:46:58AM +0100, Andreas Kostyrka wrote:

> 
> Basically, strings are immutable. If you need to append something to a
> string, you need to construct a new string object with the new value.
> 
> Now if you are using this to collect huge outputfiles in pieces, one of
> the common idioms in Python is collecting it in a list, and converting
> to string at the end:
> 
> collector = []
> for i in xrange(100000):
>     collector.append((str(i) * 80)[0:80])
> 
> string = "".join(collector)
> assert len(string) == 8000000 # ~8MB

That was formerly good advice for sure, and it probably still is
good advice.  But, read the following note from
http://docs.python.org/lib/typesseq.html:

    (6)
    If s and t are both strings, some Python implementations such as
    CPython can usually perform an in-place optimization for
    assignments of the form s=s+t or s+=t. When applicable, this
    optimization makes quadratic run-time much less likely. This
    optimization is both version and implementation dependent. For
    performance sensitive code, it is preferable to use the str.join()
    method which assures consistent linear concatenation performance
    across versions and implementations. Changed in version 2.4:
    Formerly, string concatenation never occurred in-place.

As the above note says, this optimization is implementation
dependent.  In particular, if you plan on moving your code to
Jython, then follow Andreas's suggestion to use list append
followed by string join.

- Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to