fastest string building?

Duncan Booth duncan at rcp.co.uk
Fri Oct 22 05:47:56 EDT 1999


prestonlanders at my-deja.com (Preston Landers) wrote in 
<7uieee$nsa$1 at nnrp1.deja.com>:

>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.
>
>Can someone look at my profiling script and see if I made any obvious
>errors?  Is behavior of strings already well-known?
>
>

Your strings are very short, so the actual copying takes an insignificant 
proportion of the time. Your method 2 needs to do a lot of extra work, 
looking up a global variable, calling a function etc.

Change the assignment to use longer strings in args:
        args = ("a"*1000, "b"*1000, "c"*1000, "d"*1000, "e"*1000)
and now on my machine method_3 beats method_1, with method_2 still last.

Now change the multiplier to 10000 (and reduce the number of times round 
the loop by a factor of 10) and method_1 comes last, method_3 still wins.

Finally change the definition of method_2 slightly to remove the global 
lookup:
def method_2(a, b, c, d, e, join=string.join):
    return join([a, b, c, d, e], "")

At last, with 10000 character strings the modified method_2 is the fastest.




More information about the Python-list mailing list