Skip to content

Instantly share code, notes, and snippets.

@suzaku
Last active December 22, 2015 13:58
Show Gist options
  • Save suzaku/6482112 to your computer and use it in GitHub Desktop.
Save suzaku/6482112 to your computer and use it in GitHub Desktop.
string concatenation performance experiment

method1-6 are taken from this article

method7 is added because I believe using generator expression would be faster

to run this experiment, you'll need to install line_profiler, memory_profiler and psutil first, all of which are just one pip install away.

after installing these profiling tools, we can run stest.py like this:

kernprof.py -l -v stest.py method1
python -m memory_profiler stest.py method7
from cStringIO import StringIO
import sys
loop_count = 100000
def method1():
out_str = ''
for num in xrange(loop_count):
out_str += `num`
return out_str
def method2():
from UserString import MutableString
out_str = MutableString()
for num in xrange(loop_count):
out_str += `num`
return out_str
def method3():
from array import array
char_array = array('c')
for num in xrange(loop_count):
char_array.fromstring(`num`)
return char_array.tostring()
def method4():
str_list = []
for num in xrange(loop_count):
str_list.append(`num`)
out_str = ''.join(str_list)
return out_str
def method5():
file_str = StringIO()
for num in xrange(loop_count):
file_str.write(`num`)
out_str = file_str.getvalue()
return out_str
def method6():
return ''.join([`num` for num in xrange(loop_count)])
def method7():
return ''.join(`num` for num in xrange(loop_count))
if __name__ == '__main__':
for i in sys.argv:
if i.startswith('method'):
profile(globals()[i])()
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment