Why it is so dramatical?

holger krekel pyth at devel.trillke.net
Tue Sep 3 06:03:19 EDT 2002


Christopher A. Craig wrote:
> djw <dwelch91 at nospam.attbi.com> writes:
> 
> > Stupid question:
> > 
> > If ""join() is 100-1000 X faster, why doesn't the Python interperter
> > translate s = s1 + s2 + ... into s = "".join( [ s1, s2, ... ] ) 
> > automatically?
> 
> You won't see that kind of dramatic results if you did that.  In fact,
> my testing shows it would be slower.  
> 
> s = "".join(s1, s2) is slower than s=s1+s2.

That's not really the question.  The OP wanted to build up a huge string
(for XML/XSTL-processing) in single steps.  Using

    something += otherstring

in *loops* is usually slow.  Doing

    stringlist.append(otherstring)

and at the end

    "".join(stringlist)

is orders of magnitude faster (depending on the size of the strings).  

Another speedy option is to use cStringIO.  See my example in another 
part of this discussion.

    holger




More information about the Python-list mailing list