-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathdis_loops.py
49 lines (40 loc) · 1001 Bytes
/
dis_loops.py
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
#!/usr/bin/env python3
# encoding: utf-8
#
# Copyright (c) 2009 Doug Hellmann All rights reserved.
#
"""
"""
#end_pymotw_header
import dis
import textwrap
import timeit
def add(words):
result = ''
for word in words:
result = result + word
return result
def add_inline(words):
result = ''
for word in words:
result += word
return result
def join(words):
return ''.join(words)
if __name__ == '__main__':
for fname in ['add', 'add_inline', 'join']:
print('FUNCTION:', fname, '\n')
f = globals()[fname]
dis.dis(f)
t = timeit.Timer(
'd = {fname}(words)'.format(fname=fname),
textwrap.dedent("""
from dis_loops import {fname}
words = [
l.strip()
for l in open('/usr/share/dict/words', 'rt')
]
""").format(fname=fname),
)
iterations = 3
print('TIME: {:0.4f}\n'.format(t.timeit(iterations)))