tests efficiency of string concatenation operations in 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 52 53 54 55  | #! /usr/bin/env python
""" (Q): which way to use for concatenating strings? 
    (A): run the script and find out yourself.
"""
import time
bloated_string = 'bloat me' * 10
# method 1, simple concatenation
def slow():    
    test_string = '' 
    start = time.clock()
    for i in range(1000):
        test_string += bloated_string
    end = time.clock()
    delta = float(end-start)
#    print 'slow len: ', len(test_string)
    return delta
# method 2, use list.append() and ''.join()
def fast():
    test_string = list()
    start = time.clock()
    for i in range(1000):
            test_string.append('%s' % bloated_string)
    test_string = ''.join(test_string)
    end = time.clock()
    delta = float(end-start)
#    print 'fast len: ', len(test_string)
    return delta
# method 3, use list comprehension and ''.join()
def fastest():
    test_string = bloated_string
    start = time.clock()
    test_string = ''.join([test_string for i in range(1000)])
    end = time.clock()
    delta = float(end-start)
#    print 'fastest len', len(test_string)
    return delta
if __name__ == '__main__':
    print '--- CPU TIMES ---'
    delta_slow = slow()
    print 'delta slow: {delta_slow}'.format(delta_slow=delta_slow)
    delta_fast = fast()
    print 'delta fast: {delta_fast}'.format(delta_fast=delta_fast)
    delta_fastest = fastest()
    print 'delta fastest: {delta_fastest}'.format(delta_fastest=delta_fastest)
    print '---'
    
    print 'listcomps is %f times faster than (list.append + ''.join())' % \
            (delta_fast/delta_fastest)
    print 'the latter is %f times faster (slower) than simple concat' %\
            (delta_slow/delta_fast)
 | 
Download
Copy to clipboard
Why do you have fast() do a string interpolation?:
Correcting for that, list comprehensions are about 2x faster than loops, and joins are almost 2x faster than looped string
+concatenations.I stand corrected. Yes, %s takes some delta from .append() and then join. Thank you for the correction.