-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
91 lines (79 loc) · 2.7 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
import time
import numpy as np
import numba
from FastLine import Line
def benchmark_fastline():
start_time = time.time()
l1 = Line(p1=(0, 0), p2=(10, 10))
l2 = Line(m=4, b=-1)
for _ in range(1000000):
l1.intersection(l2)
end_time = time.time()
return end_time - start_time
def benchmark_pure_python():
def intersection(l1, l2):
x1, y1, x2, y2 = l1
x3, y3, x4, y4 = l2
denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
if denom == 0:
return None
px = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / denom
py = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denom
return px, py
start_time = time.time()
l1 = (0, 0, 10, 10)
l2 = (0, -1, 10, 39)
for _ in range(1000000):
intersection(l1, l2)
end_time = time.time()
return end_time - start_time
def benchmark_numpy():
def intersection(l1, l2):
x1, y1, x2, y2 = l1
x3, y3, x4, y4 = l2
denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
if denom == 0:
return None
px = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / denom
py = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denom
return px, py
start_time = time.time()
l1 = np.array([0, 0, 10, 10])
l2 = np.array([0, -1, 10, 39])
for _ in range(1000000):
intersection(l1, l2)
end_time = time.time()
return end_time - start_time
@numba.jit(nopython=True)
def numba_intersection(l1, l2):
x1, y1, x2, y2 = l1
x3, y3, x4, y4 = l2
denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
if denom == 0:
return None
px = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / denom
py = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denom
return px, py
def benchmark_numba():
start_time = time.time()
l1 = np.array([0, 0, 10, 10])
l2 = np.array([0, -1, 10, 39])
for _ in range(1000000):
numba_intersection(l1, l2)
end_time = time.time()
return end_time - start_time
def main():
print("Benchmarking FastLine...")
fastline_time = benchmark_fastline()
print(f"FastLine: {fastline_time:.4f} seconds")
print("Benchmarking Pure Python...")
pure_python_time = benchmark_pure_python()
print(f"Pure Python: {pure_python_time:.4f} seconds")
print("Benchmarking NumPy...")
numpy_time = benchmark_numpy()
print(f"NumPy: {numpy_time:.4f} seconds")
print("Benchmarking Numba...")
numba_time = benchmark_numba()
print(f"Numba: {numba_time:.4f} seconds")
if __name__ == "__main__":
main()