Welcome, guest | Sign In | My Account | Store | Cart

tests efficiency of string concatenation operations in python

Python, 55 lines
 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)

2 comments

Kevin Edwards 9 years, 8 months ago  # | flag

Why do you have fast() do a string interpolation?:

'%s' % bloated_string

Correcting for that, list comprehensions are about 2x faster than loops, and joins are almost 2x faster than looped string + concatenations.

akhil jaggarwal (author) 9 years, 8 months ago  # | flag

I stand corrected. Yes, %s takes some delta from .append() and then join. Thank you for the correction.