Skip to content

Instantly share code, notes, and snippets.

@sapamja
Last active August 29, 2015 14:00
Show Gist options
  • Save sapamja/11075693 to your computer and use it in GitHub Desktop.
Save sapamja/11075693 to your computer and use it in GitHub Desktop.
Fastest way to concatenate string in python
#!/usr/bin/python
import cProfile
from faker import Faker
from timeit import Timer
from UserString import MutableString
from cStringIO import StringIO
#from memory_profiler import profile
def func1(List):
"""Naive appending"""
final_string = ''
for string in List:
final_string += string
return final_string
def func2(List):
"""Using mutable string"""
final_string = MutableString()
for string in List:
final_string += string
return final_string
def func3(List):
"""Build string using join"""
return ''.join(List)
def func4(List):
"""Write to memory"""
final_string = StringIO()
for num in List:
final_string.write(num)
return final_string.getvalue()
def func5(List):
"""List comprehensions with join"""
return ''.join([char for char in List])
s = Faker()
func = [ func1, func2, func3, func4, func5 ]
Lis = [s.random_letter() for x in range(10000) ]
d = dict()
for fun in func:
t = Timer(lambda: fun(Lis))
# print fun.__name__, cProfile.run('t.timeit(number=100)')
d[fun.__doc__] = t.timeit(number=1000)
print 'Testing on 10000 characters with 1000 repeat'
for x in [(x, d[x]) for x in sorted(d, key=d.get)]:
print x
func1 211 function calls in 0.114 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
100 0.000 0.000 0.113 0.001 conc.py:45(<lambda>)
100 0.113 0.001 0.113 0.001 conc.py:9(func1)
1 0.000 0.000 0.000 0.000 timeit.py:143(setup)
1 0.000 0.000 0.114 0.114 timeit.py:178(timeit)
1 0.000 0.000 0.114 0.114 timeit.py:96(inner)
1 0.000 0.000 0.000 0.000 {gc.disable}
1 0.000 0.000 0.000 0.000 {gc.enable}
1 0.000 0.000 0.000 0.000 {gc.isenabled}
1 0.000 0.000 0.000 0.000 {globals}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
2 0.000 0.000 0.000 0.000 {time.time}
None
func2 7000441 function calls (7000440 primitive calls) in 5.897 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 5.897 5.897 <string>:1(<module>)
100 0.001 0.000 0.001 0.000 UserString.py:148(__init__)
1000000 1.288 0.000 5.218 0.000 UserString.py:206(__iadd__)
2 0.000 0.000 0.000 0.000 _abcoll.py:97(__subclasshook__)
2 0.000 0.000 0.000 0.000 _weakrefset.py:16(__init__)
2 0.000 0.000 0.000 0.000 _weakrefset.py:20(__enter__)
2 0.000 0.000 0.000 0.000 _weakrefset.py:26(__exit__)
2 0.000 0.000 0.000 0.000 _weakrefset.py:36(__init__)
2 0.000 0.000 0.000 0.000 _weakrefset.py:52(_commit_removals)
2 0.000 0.000 0.000 0.000 _weakrefset.py:58(__iter__)
2000001 1.040 0.000 1.040 0.000 _weakrefset.py:68(__contains__)
2 0.000 0.000 0.000 0.000 _weakrefset.py:81(add)
1000000 1.855 0.000 3.053 0.000 abc.py:128(__instancecheck__)
2/1 0.000 0.000 0.000 0.000 abc.py:148(__subclasscheck__)
100 0.678 0.007 5.897 0.059 conc.py:16(func2)
100 0.000 0.000 5.897 0.059 conc.py:45(<lambda>)
1 0.000 0.000 0.000 0.000 timeit.py:143(setup)
1 0.000 0.000 5.897 5.897 timeit.py:178(timeit)
1 0.000 0.000 5.897 5.897 timeit.py:96(inner)
100 0.000 0.000 0.000 0.000 warnings.py:14(warnpy3k)
1 0.000 0.000 0.000 0.000 {gc.disable}
1 0.000 0.000 0.000 0.000 {gc.enable}
1 0.000 0.000 0.000 0.000 {gc.isenabled}
1000002 0.158 0.000 0.158 0.000 {getattr}
1 0.000 0.000 0.000 0.000 {globals}
2000000 0.877 0.000 3.930 0.000 {isinstance}
1 0.000 0.000 0.000 0.000 {issubclass}
2 0.000 0.000 0.000 0.000 {method '__subclasses__' of 'type' objects}
4 0.000 0.000 0.000 0.000 {method 'add' of 'set' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
2 0.000 0.000 0.000 0.000 {method 'remove' of 'set' objects}
2 0.000 0.000 0.000 0.000 {time.time}
None
func3 311 function calls in 0.013 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.013 0.013 <string>:1(<module>)
100 0.000 0.000 0.013 0.000 conc.py:23(func3)
100 0.000 0.000 0.013 0.000 conc.py:45(<lambda>)
1 0.000 0.000 0.000 0.000 timeit.py:143(setup)
1 0.000 0.000 0.013 0.013 timeit.py:178(timeit)
1 0.000 0.000 0.013 0.013 timeit.py:96(inner)
1 0.000 0.000 0.000 0.000 {gc.disable}
1 0.000 0.000 0.000 0.000 {gc.enable}
1 0.000 0.000 0.000 0.000 {gc.isenabled}
1 0.000 0.000 0.000 0.000 {globals}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
100 0.013 0.000 0.013 0.000 {method 'join' of 'str' objects}
2 0.000 0.000 0.000 0.000 {time.time}
None
func4 1000411 function calls in 0.432 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.432 0.432 <string>:1(<module>)
100 0.258 0.003 0.432 0.004 conc.py:27(func4)
100 0.000 0.000 0.432 0.004 conc.py:45(<lambda>)
1 0.000 0.000 0.000 0.000 timeit.py:143(setup)
1 0.000 0.000 0.432 0.432 timeit.py:178(timeit)
1 0.000 0.000 0.432 0.432 timeit.py:96(inner)
100 0.000 0.000 0.000 0.000 {cStringIO.StringIO}
1 0.000 0.000 0.000 0.000 {gc.disable}
1 0.000 0.000 0.000 0.000 {gc.enable}
1 0.000 0.000 0.000 0.000 {gc.isenabled}
1 0.000 0.000 0.000 0.000 {globals}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
100 0.000 0.000 0.000 0.000 {method 'getvalue' of 'cStringIO.StringO' objects}
1000000 0.173 0.000 0.173 0.000 {method 'write' of 'cStringIO.StringO' objects}
2 0.000 0.000 0.000 0.000 {time.time}
None
func5 311 function calls in 0.054 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.054 0.054 <string>:1(<module>)
100 0.041 0.000 0.054 0.001 conc.py:34(func5)
100 0.000 0.000 0.054 0.001 conc.py:45(<lambda>)
1 0.000 0.000 0.000 0.000 timeit.py:143(setup)
1 0.000 0.000 0.054 0.054 timeit.py:178(timeit)
1 0.000 0.000 0.054 0.054 timeit.py:96(inner)
1 0.000 0.000 0.000 0.000 {gc.disable}
1 0.000 0.000 0.000 0.000 {gc.enable}
1 0.000 0.000 0.000 0.000 {gc.isenabled}
1 0.000 0.000 0.000 0.000 {globals}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
100 0.013 0.000 0.013 0.000 {method 'join' of 'str' objects}
2 0.000 0.000 0.000 0.000 {time.time}
None
Output:
Testing on 10000 characters with 1000 repeat
('Build string using join', 0.12692713737487793)
('List comprehensions with join', 0.5768389701843262)
('Naive appending', 1.169692039489746)
('Write to memory', 2.993551015853882)
('Using mutable string', 45.05538487434387)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment