-
Notifications
You must be signed in to change notification settings - Fork 12
/
perf.py
114 lines (65 loc) · 2.04 KB
/
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
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())
print("dispatch 0 args")
print(timeit.timeit("docall0()", setup="from __main__ import docall0"))
print("dispatch 1 args")
print(timeit.timeit("docall1()", setup="from __main__ import docall1"))
print("dispatch 2 args")
print(timeit.timeit("docall2()", setup="from __main__ import docall2"))
print("dispatch 3 args")
print(timeit.timeit("docall3()", setup="from __main__ import docall3"))
print("dispatch 4 args")
print(timeit.timeit("docall4()", setup="from __main__ import docall4"))
print("Plain func 0 args")
print(timeit.timeit("plain_docall0()", setup="from __main__ import plain_docall0"))
print("Plain func 4 args")
print(timeit.timeit("plain_docall4()", setup="from __main__ import plain_docall4"))