forked from fluentpython/example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_concat.py
More file actions
43 lines (36 loc) · 1023 Bytes
/
str_concat.py
File metadata and controls
43 lines (36 loc) · 1023 Bytes
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
"""string concatenation demos"""
from time import perf_counter
def load_lines():
with open('war-and-peace.txt') as fp:
return fp.readlines() * 100 # replace with 200 or more for surprises!!!
def chrono(f):
def inner(lines):
t0 = perf_counter()
text = f(lines)
elapsed = perf_counter() - t0
print('%15s: %fs' % (f.__name__, elapsed))
return text
return inner
@chrono
def iadd_joiner(lines):
text = ''
for line in lines:
text += line
return text
@chrono
def list_joiner(lines):
parts = []
for line in lines:
parts.append(line)
return ''.join(parts)
@chrono
def genexp_joiner(lines):
return ''.join(line for line in lines)
if __name__=='__main__':
lines = load_lines()
print('joining %s lines' % len(lines))
text0 = iadd_joiner(lines)
text1 = list_joiner(lines)
text2 = genexp_joiner(lines)
assert len(text0) == len(text1) == len(text2), repr(
(len(text0), len(text1), len(text2)))