-
Notifications
You must be signed in to change notification settings - Fork 5
/
benchmark.py
50 lines (36 loc) · 877 Bytes
/
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
from time import time
import autograd.numpy as np
import lab as B
n = 20
m = 1
t = np.float64
eps = B.cast(t, B.epsilon)
def f1(x):
dists2 = (x - B.transpose(x)) ** 2
K = B.exp(-0.5 * dists2)
K = K + B.epsilon * B.eye(t, n)
L = B.cholesky(K)
return B.matmul(L, B.ones(t, n, m))
def f2(x):
dists2 = (x - np.transpose(x)) ** 2
K = np.exp(-0.5 * dists2)
K = K + B.epsilon * np.eye(n, dtype=t)
L = np.linalg.cholesky(K)
return np.matmul(L, np.ones((n, m)))
# Perform computation once.
x = np.linspace(0, 1, n, dtype=t)[:, None]
f1(x)
f2(x)
its = 10000
s = time()
for _ in range(its):
z = f2(x)
us_native = (time() - s) / its * 1e6
s = time()
for _ in range(its):
z = f1(x)
us_lab = (time() - s) / its * 1e6
print(
"Overhead: {:.1f} us / {:.1f} %"
"".format(us_lab - us_native, 100 * (us_lab / us_native - 1))
)