-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.py
More file actions
117 lines (82 loc) · 1.61 KB
/
switch.py
File metadata and controls
117 lines (82 loc) · 1.61 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from __future__ import unicode_literals
from __future__ import print_function
import time
from operator import attrgetter
S1 = 'a'
S2 = 'b'
S3 = 'c'
S4 = 'd'
S5 = 'e'
S6 = 'f'
def s1(arg):
pass
def s2(arg):
pass
def s3(arg):
pass
def s4(arg):
pass
def s5(arg):
pass
def s6(arg):
pass
def ifelif(arg):
if arg == S1:
s1(arg)
elif arg == S2:
s2(arg)
elif arg == S3:
s3(arg)
elif arg == S4:
s4(arg)
elif arg == S5:
s5(arg)
elif arg == S6:
s6(arg)
dispatch = {
S1: s1,
S2: s2,
S3: s3,
S4: s4,
S5: s5,
S6: s6}
def dictdispatch(arg):
dispatch[arg](arg)
def dictdefinedispatch(arg):
defdispatch = {
S1: s1,
S2: s2,
S3: s3,
S4: s4,
S5: s5,
S6: s6}
defdispatch[arg](arg)
class DictAttrGetter(object):
def attrgettrdispatch(self, arg):
method = 'case_' + arg
getattr(self, method)(arg)
def case_a(self, arg):
pass
def case_b(self, arg):
pass
def case_c(self, arg):
pass
def case_d(self, arg):
pass
def case_e(self, arg):
pass
def case_f(self, arg):
pass
def speed_test(f):
lst = list(range(100000))
start = time.time()
for x in lst:
for x in (S1, S1, S1, S1, S2, S2, S2, S3, S3, S4, S5, S6):
f(x)
print(f.__name__, time.time() - start)
if __name__ == '__main__':
speed_test(ifelif)
speed_test(dictdefinedispatch)
speed_test(dictdispatch)
DAG = DictAttrGetter()
speed_test(DAG.attrgettrdispatch)