-
Notifications
You must be signed in to change notification settings - Fork 38
/
examples.py
119 lines (94 loc) · 3.14 KB
/
examples.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
115
116
117
118
119
import numpy as np
import time
from hilbertcurve.hilbertcurve import HilbertCurve
do_multiprocessing_test = True
# When using a single iteration (p=1) in 2 dimensions (N=2) there are only 4
# locations on the curve
# distance | coordinates
# 0 | [0, 0]
# 1 | [0, 1]
# 2 | [1, 1]
# 3 | [1, 0]
# calculate distances along a hilbert curve given points
p = 1
n = 2
hilbert_curve = HilbertCurve(p, n)
points = [[0,0], [0,1], [1,1], [1,0]]
dists = hilbert_curve.distances_from_points(points)
print("simple distances from points")
print("="*80)
for point, dist in zip(points, dists):
print(f'distance(x={point}, p={p}, n={n}) = {dist}')
print()
# calculate coordinates given distances along a hilbert curve
p = 1
n = 2
hilbert_curve = HilbertCurve(p, n)
dists = list(range(4))
points = hilbert_curve.points_from_distances(dists)
print("simple points from distances")
print("="*80)
for point, dist in zip(points, dists):
print(f'point(h={dist}, p={p}, n={n}) = {point}')
print()
# due to the magic of arbitrarily large integers in
# Python (https://www.python.org/dev/peps/pep-0237/)
# these calculations can be done with absurd numbers
p = 512
n = 10
hilbert_curve = HilbertCurve(p, n)
ii = 123456789101112131415161718192021222324252627282930
points = hilbert_curve.points_from_distances([ii])
print("arbitrarily large integer points from distances")
print("="*80)
for point, dist in zip(points, dists):
print(f'point(h={ii}, p={p}, n={n}) = {point}')
print()
points = [
[121075, 67332, 67326, 108879, 26637, 43346, 23848, 1551, 68130, 84004]
]
points_check = list(points)
dists = hilbert_curve.distances_from_points(points)
print("arbitrarily large integer distances from points")
print("="*80)
for point, dist in zip(points, dists):
print(f'distance(x={point}, p={p}, n={n}) = {dist}')
print()
assert(points == points_check)
if do_multiprocessing_test:
p = 8
n = 7
print("speed test single core")
print("="*80)
hilbert_curve = HilbertCurve(p, n)
for num_points in [1_000, 10_000, 100_000, 1_000_000]:
t1 = time.time()
points = np.random.randint(
low=0,
high=hilbert_curve.max_x + 1,
size=(num_points, hilbert_curve.n)
)
t2 = time.time()
print("created {} points in {:.3f}".format(num_points, t2-t1))
t1 = time.time()
distances = hilbert_curve.distances_from_points(points)
t2 = time.time()
print("calculated {} distances in {:.3f}".format(num_points, t2-t1))
print()
print("speed test multi core")
print("="*80)
hilbert_curve = HilbertCurve(p, n, n_procs=-1)
for num_points in [1_000, 10_000, 100_000, 1_000_000]:
t1 = time.time()
points = np.random.randint(
low=0,
high=hilbert_curve.max_x + 1,
size=(num_points, hilbert_curve.n)
)
t2 = time.time()
print("created {} points in {:.3f}".format(num_points, t2-t1))
t1 = time.time()
distances = hilbert_curve.distances_from_points(points)
t2 = time.time()
print("calculated {} distances in {:.3f}".format(num_points, t2-t1))
print()