fastest string building?

Aahz Maruch aahz at netcom.com
Tue Oct 19 15:22:30 EDT 1999


In article <7uieee$nsa$1 at nnrp1.deja.com>,
Preston Landers  <prestonlanders at my-deja.com> wrote:
>
> I was recently wondering what the fastest way to work with strings
>was.  For instance, let's say I have a string, and I want to add some
>things to the end, or possibly insert them in the middle.  If I were to
>repeat that thousands or hundreds of thousands of times, what would be
>the most efficient syntax to do it?
>
>To my knowledge, there are basically three ways to do it.
>
>1: s = part1 + part2 + part3 + ... partn
>
>2: s = string.join([part1, part2, partn], "")
>
>3: s = "%s%s%s%s" % (part1, part2, part3, part4)
>
>Am I missing anything?

cStringIO

>Just now, however, I decided to do a more indepth test with the Python
>profiler.  The results were surprising: method #1 above was fastest,
>followed closely by #3, and #2 lagged way back.  So, according to this
>test, s = part1 + part2 + partn seems to be the fastest way to build a
>string out of parts.

That's probably true.  The tricky part is that "%" gives you a lot of
flexibility and simplicity for building strings, so it's almost always
better if you have a moderately complex string to build.

Problem is this: most of the time when speed becomes an issue, you're
building strings in loops.  Once a loop is involved, the copying
overhead of "s=s+part" becomes non-trivial.  At that point, either
string.join() or cStringIO become the best solution.
--
                      --- Aahz (@netcom.com)

Androgynous poly kinky vanilla queer het    <*>      http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6  (if you want to know, do some research)




More information about the Python-list mailing list