-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__main__.py
More file actions
56 lines (43 loc) · 1.25 KB
/
__main__.py
File metadata and controls
56 lines (43 loc) · 1.25 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
import timeit
from functools import partial
import numpy as np
from fast_frechet import (
accumulate,
branchless,
compiled,
linear_memory,
no_recursion,
reduce_accumulate,
reduce_accumulate2,
vectorized,
)
def metric(p, q):
dx = p[..., 0] - q[..., 0]
dy = p[..., 1] - q[..., 1]
return np.hypot(dx, dy)
def generate_trajectory(n, *, rng):
xy0 = rng.integers(-2, 2, size=(1, 2), endpoint=False).astype(np.float64)
dxy = rng.integers(-1, 1, size=(n, 2), endpoint=False).astype(np.float64)
return xy0 + np.cumsum(dxy, axis=0)
def benchmark(f, *, n, rng):
p = generate_trajectory(n, rng=rng)
q = generate_trajectory(n, rng=rng)
return min(timeit.repeat(lambda: f(p, q), repeat=3, number=1)) * 1_000
def main(*, n=1024, seed=42):
print(f"Length of trajectory = {n}")
print("")
for v in [
no_recursion,
vectorized,
branchless,
linear_memory,
accumulate,
reduce_accumulate,
reduce_accumulate2,
compiled,
]:
f = partial(v.frechet_distance, metric=metric)
t = benchmark(f, n=n, rng=np.random.default_rng(seed))
print(f"{v.__name__.split('.')[-1]:>20}: {t:>4.0f} ms")
if __name__ == "__main__":
main()