-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeit_examples.py
77 lines (61 loc) · 1.3 KB
/
timeit_examples.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import timeit
import argparse
def time_them(them):
for s in (them):
print("\n=========")
print(s)
print("---------")
print(timeit.timeit(stmt=s, number=1000))
print("=========")
s1 = """\
x = []
for i in xrange(0, 10000):
x.append(i)
"""
s2 = "x = [i for i in xrange(0, 10000)]"
s3 = """\
foo = {}
for x in xrange(0, 100):
for y in xrange(0, 100):
foo["{}:{}".format(x, y)] = (y, x)
"""
s4 = """\
foo = {}
for x in xrange(0, 100):
for y in xrange(0, 100):
foo[":".join([str(x), str(y)])] = (y, x)
"""
s5 = """\
foo = dict()
for x in xrange(0, 100):
for y in xrange(0, 100):
foo[(x, y)] = (y, x)
"""
s6 = """\
foo = []
for x in xrange(0, 10000):
foo.append(x)
"""
s7 = """\
foo = []
foo_append = foo.append
for x in xrange(0, 10000):
foo_append(x)
"""
sets = {
'comprehension': [s1, s2],
'dictkeys': [s3, s4, s5],
'dots': [s6, s7],
}
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="run timeit examples"
)
parser.add_argument("timeit_set",
type=str,
help="The timeit set to run")
args = parser.parse_args()
if args.timeit_set in sets:
time_them(sets[args.timeit_set])
else:
print('Unknown set"')