-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtox_perf.py
157 lines (106 loc) · 2.57 KB
/
tox_perf.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import timeit
from reg import dispatch
from reg import DictCachingKeyLookup
def get_key_lookup(r):
return DictCachingKeyLookup(r)
@dispatch(get_key_lookup=get_key_lookup)
def args0():
raise NotImplementedError()
@dispatch("a", get_key_lookup=get_key_lookup)
def args1(a):
raise NotImplementedError()
@dispatch("a", "b", get_key_lookup=get_key_lookup)
def args2(a, b):
raise NotImplementedError()
@dispatch("a", "b", "c", get_key_lookup=get_key_lookup)
def args3(a, b, c):
raise NotImplementedError()
@dispatch("a", "b", "c", "d", get_key_lookup=get_key_lookup)
def args4(a, b, c, d):
raise NotImplementedError()
class Foo:
pass
def myargs0():
return "args0"
def myargs1(a):
return "args1"
def myargs2(a, b):
return "args2"
def myargs3(a, b, c):
return "args3"
def myargs4(a, b, c, d):
return "args4"
args0.register(myargs0)
args1.register(myargs1, a=Foo)
args2.register(myargs2, a=Foo, b=Foo)
args3.register(myargs3, a=Foo, b=Foo, c=Foo)
args4.register(myargs4, a=Foo, b=Foo, c=Foo, d=Foo)
def docall0():
args0()
def docall1():
args1(Foo())
def docall2():
args2(Foo(), Foo())
def docall3():
args3(Foo(), Foo(), Foo())
def docall4():
args4(Foo(), Foo(), Foo(), Foo())
def plain_docall0():
myargs0()
def plain_docall4():
myargs4(Foo(), Foo(), Foo(), Foo())
plain_zero_time = timeit.timeit(
"plain_docall0()", setup="from __main__ import plain_docall0"
)
print("\nPerformance test")
print("================")
print("dispatch 0 args")
print(
"{:.2f}".format(
timeit.timeit("docall0()", setup="from __main__ import docall0")
/ plain_zero_time
)
+ "x"
)
print("dispatch 1 args")
print(
"{:.2f}".format(
timeit.timeit("docall1()", setup="from __main__ import docall1")
/ plain_zero_time
)
+ "x"
)
print("dispatch 2 args")
print(
"{:.2f}".format(
timeit.timeit("docall2()", setup="from __main__ import docall2")
/ plain_zero_time
)
+ "x"
)
print("dispatch 3 args")
print(
"{:.2f}".format(
timeit.timeit("docall3()", setup="from __main__ import docall3")
/ plain_zero_time
)
+ "x"
)
print("dispatch 4 args")
print(
"{:.2f}".format(
timeit.timeit("docall4()", setup="from __main__ import docall4")
/ plain_zero_time
)
+ "x"
)
print("Plain func 0 args")
print("1.00x (base duration)")
print("Plain func 4 args")
print(
"{:.2f}".format(
timeit.timeit("plain_docall4()", setup="from __main__ import plain_docall4")
/ plain_zero_time
)
+ "x"
)