-
Notifications
You must be signed in to change notification settings - Fork 24
/
benchmark.py
103 lines (68 loc) · 1.87 KB
/
benchmark.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
from typing import Tuple
import numpy as np
from tests.util import benchmark
from plum import Dispatcher, dispatch
def f(x):
pass
@dispatch
def g(x: int):
pass
@dispatch
def g(x: str):
pass
dur_native = benchmark(f, (1,), n=1000, burn=10)
dur_plum = benchmark(g, (1,), n=1000, burn=10)
factor = int(np.round(dur_plum / dur_native))
print("# Function Calls")
print(f"Native call: {dur_native:6.2f} us ({1:.1f} x)")
print(f"Plum call: {dur_plum:6.2f} us ({factor:.1f} x)")
print()
def f2(x):
pass
@dispatch
def g2(x: Tuple[int]):
pass
@dispatch
def g2(x: Tuple[str]):
pass
dur_native = benchmark(f2, ((1,),), n=1000, burn=10)
dur_plum = benchmark(g2, ((1,),), n=1000, burn=10)
factor = int(np.round(dur_plum / dur_native))
print("# Parametric Function Calls")
print(f"Native call: {dur_native:6.2f} us ({1:.1f} x)")
print(f"Plum call: {dur_plum:6.2f} us ({factor:.1f} x)")
print()
class A:
def __call__(self, x):
pass
def go(self, x):
pass
class B:
_dispatch = Dispatcher()
@_dispatch
def __call__(self, x: int):
pass
@_dispatch
def __call__(self, x: str):
pass
@_dispatch
def go(self, x: int):
pass
@_dispatch
def go(self, x: str):
pass
a = A()
b = B()
dur_native = benchmark(a, (1,), n=1000, burn=10)
dur_plum = benchmark(b, (1,), n=1000, burn=10)
factor = int(np.round(dur_plum / dur_native))
print("# Class Calls")
print(f"Native call: {dur_native:6.2f} us ({1:.1f} x)")
print(f"Plum call: {dur_plum:6.2f} us ({factor:.1f} x)")
print()
dur_native = benchmark(lambda x: a.go(x), (1,), n=1000, burn=10)
dur_plum = benchmark(lambda x: b.go(x), (1,), n=1000, burn=10)
factor = int(np.round(dur_plum / dur_native))
print("# Class Attribute Calls")
print(f"Native call: {dur_native:6.2f} us ({1:.1f} x)")
print(f"Plum call: {dur_plum:6.2f} us ({factor:.1f} x)")