Fastest technique for string concatenation

python at bdurham.com python at bdurham.com
Sat Oct 2 22:20:22 CEST 2010


Emile,

> Your times will improve when not burdened by the repeated method lookups and element-wise list creation.

Excellent point!!

Here's updated timings for each technique followed by copy and paste
source code for anyone that wants to fiddle with these tests. I've
placed your name above your script suggestions.

My results (Python 2.7/32-bit on Window 7/64-bit)

testListAppend = 10.95 sec
testStringConcat = 49.49 sec
testStringIO = 14.68 sec
testListAppend2 = 7.42 sec <-------- fastest !!!
testListAppend3 = 8.22 sec
testStringIO2 = 10.91 sec

Thanks for the feedback - really appreciated.

Malcolm


# test performance of various string concatenation techniques

import cStringIO
import timeit

source = 'x' * 5000000

def testListAppend():
	output = list()
	for char in source:
		output.append( char )
	output = ''.join( output )
	
def testStringConcat():
	output = ''
	for char in source:
		output += char
	
def testStringIO():     
	output = cStringIO.StringIO()
	for char in source:
		output.write( char )
	output = output.getvalue()


# "Emile van Sebille" <emile at fenx.com>:
# Your times will improve when not burdened by the repeated method
lookups and element-wise list creation:

def testListAppend2():
     output = list()
     append = output.append
     for char in source:
         append( char )
     output = ''.join( output )

def testListAppend3():
     input = list( source )
     output = list()
     append = output.append
     for char in input:
         append( char )
     output = ''.join( output )

def testStringIO2():
     output = cStringIO.StringIO()
     write = output.write
     for char in source:
         write( char )
     output = output.getvalue()
	
def time( func ):
	timingObject = timeit.Timer( func )
	runtime = timingObject.timeit( 10 )
	print '%s = %.2f sec' % ( func.__name__, runtime )

time( testListAppend )
time( testStringConcat )
time( testStringIO )
time( testListAppend2 )
time( testListAppend3 )
time( testStringIO2 )



More information about the Python-list mailing list