Test strings concatenation print one two print format one two print on

 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
""" Test strings concatenation
>>> print 'one' + ' ' + 'two'
>>> print "{} {}".format('one', 'two')
>>> print "%s %s" % ('one', 'two') <== outdated
>>> print ' '.join(['one', 'two'])
"""
import timeit
import random
import string
def generate_random_strings(number=6, length=120):
for index in range(number):
#yield ''.join(random.sample(string.ascii_letters, length))
yield ''.join([random.choice(string.ascii_letters) for _ in range(length)])
RANDOM_STR_LIST = list(generate_random_strings())
def func1(str_list=RANDOM_STR_LIST):
result = ''
for s in str_list:
result += s
return result
def func2(str_list=RANDOM_STR_LIST):
result = '{}{}'
for s in str_list:
result.format(result, s)
return result
def func3(str_list=RANDOM_STR_LIST):
result = ''.join(str_list)
return result
if __name__ == '__main__':
print RANDOM_STR_LIST
print "func1: {}".format(
timeit.timeit(func1, number=100000)
)
print "func2: {}".format(
timeit.timeit(func3, number=100000)
)
print "func3: {}".format(
timeit.timeit(func3, number=100000)
)