• Download Gist
python string concat test
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#!/usr/bin/env python
 
def test_method(method):
start_time = time.time()
result = method()
assert len(result) == 68888890, 'Method work no good: %s != 68888890' % len(result)
end_time = time.time()
 
print method.func_name, end_time - start_time
 
 
def naive_way():
accum = ''
for x in data:
accum += str(x)
 
return accum
 
def join_way():
return ''.join([str(x) for x in data])
 
def join_for_way():
accum = []
for x in data:
accum.append(str(x))
 
return ''.join(accum)
 
from UserString import MutableString
def mutable_way():
accum = MutableString()
for x in data:
accum += str(x)
 
return accum
 
from cStringIO import StringIO
def stringio_way():
f = StringIO()
for x in data:
f.write(str(x))
return f.getvalue()
 
 
test_method(naive_way)
test_method(join_way)
test_method(join_for_way)
#test_method(mutable_way)
test_method(stringio_way)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.