Some of the examples in that SO post are outdated. However, list joining is faster that string concatenation, but not by much. Assembling a 110 MB string:
from timeit import Timer
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
nr = 1200000
data = "The Quick Brown Fox Jumps Over The Lazy Dog: Woven silk pyjamas exchanged for blue quartz.\n"
# contruct a list first, then join
def dolist():
s = []
a = s.append
i = 0
while i < nr:
a(data)
i += 1
s = "".join(s)
print("%s chars (joined list)" % len(s))
# string concatenation fest
def dostr():
s = ""
i = 0
while i < nr:
s += data
i += 1
print("%s chars (string concatenation)" % len(s))
# use a string as a file
def dostringio():
buf = StringIO()
w = buf.write
i = 0
while i < nr:
w(data)
i += 1
s = buf.getvalue()
print("%s chars (cStringIO)" % len(s))
if 1:
tlist = Timer("dolist()", "from __main__ import dolist")
print("the joined list took %.2f seconds" % tlist.timeit(2))
tstr = Timer("dostr()", "from __main__ import dostr")
print("the concatenation fest took %.2f seconds" % tstr.timeit(2))
tlist = Timer("dostringio()", "from __main__ import dostringio")
print("the cStringIO approach took %.2f seconds" % tlist.timeit(2))
else:
@profile
def callall():
# For use with a profiler (eg, kernprof.py/lineprof)
for i in xrange(2):
dolist()
dostr()
dostringio()
callall()
Result:
(user@air) /Users/user/Prj/python $ python3 stringplakbenchmark.py
109200000 chars (joined list)
109200000 chars (joined list)
the joined list took 1.12 seconds
109200000 chars (string concatenation)
109200000 chars (string concatenation)
the concatenation fest took 1.76 seconds
109200000 chars (cStringIO)
109200000 chars (cStringIO)
the cStringIO approach took 1.45 seconds
(user@air) /Users/user/Prj/python $ python2.7 stringplakbenchmark.py
109200000 chars (joined list)
109200000 chars (joined list)
the joined list took 0.99 seconds
109200000 chars (string concatenation)
109200000 chars (string concatenation)
the concatenation fest took 1.33 seconds
109200000 chars (cStringIO)
109200000 chars (cStringIO)
the cStringIO approach took 5.21 seconds
(user@air) /Users/user/Prj/python $ python2.6 stringplakbenchmark.py
109200000 chars (joined list)
109200000 chars (joined list)
the joined list took 0.95 seconds
109200000 chars (string concatenation)
109200000 chars (string concatenation)
the concatenation fest took 1.39 seconds
109200000 chars (cStringIO)
109200000 chars (cStringIO)
the cStringIO approach took 5.54 seconds