-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.nim
184 lines (146 loc) · 5.73 KB
/
bench.nim
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import std/[math, monotimes, random, strutils, terminal, times]
import ../src/delaunator
proc uniform[T](count: int): seq[array[2, T]] =
var points: seq[array[2, T]] = newSeqOfCap[array[2, T]](count)
for i in 0 ..< count:
points.add([rand[T](T(0.0) .. T(1.0)) * T(1e3), rand[T](T(0.0) .. T(1.0)) * T(1e3)])
return points
proc gaussian[T](count: int): seq[array[2, T]] =
var points: seq[array[2, T]] = newSeqOfCap[array[2, T]](count)
for i in 0 ..< count:
points.add([T(gauss() * 1e3), T(gauss() * 1e3)])
return points
proc grid[T](count: int): seq[array[2, T]] =
var points: seq[array[2, T]] = newSeqOfCap[array[2, T]](count)
let size = sqrt(T(count))
for i in 0 ..< int(floor(size)):
for j in 0 ..< int(floor(size)):
points.add([T(i), T(j)])
return points
proc degenerate[T](count: int): seq[array[2, T]] =
var points: seq[array[2, T]] = newSeqOfCap[array[2, T]](count + 1)
points.add([T(0.0), T(0.0)])
for i in 0 ..< count:
let angle = 2.0 * PI * T(i) / T(count)
points.add([T(1e10 * sin(angle)), T(1e10 * cos(angle))])
return points
proc triangulateCoords[T](points: var seq[T]): Delaunator[T] =
return delaunator.fromCoords[T](points)
proc triangulatePoints[P, T](points: var seq[array[2, T]]): Delaunator[T] =
return delaunator.fromPoints[P, T](points)
when isMainModule:
let
distNames = @["uniform", "gaussian", "grid", "degenerate"]
f32Distributions = @[uniform[float32], gaussian[float32], grid[float32], degenerate[float32]]
f64Distributions = @[uniform[float64], gaussian[float64], grid[float64], degenerate[float64]]
counts = @[20_000, 100_000, 200_000, 500_000, 1_000_000]
randomize()
echo ""
writeStyled(" ")
writeStyled("n-points:", {styleDim, styleUnderscore, styleItalic})
writeStyled(" 20k 100k 200k 500k 1000k\n", {styleDim, styleUnderscore, styleItalic})
echo ""
# bench float32 points
writeStyled(" fromPoints (float32):\n", {styleBright})
for i in 0 ..< distNames.len:
writeStyled(" " & align(distNames[i], 10) & ":", {styleDim})
let generate = f32Distributions[i]
# warmup
var gend = generate(counts[0])
discard triangulatePoints[array[2, float32], float32](gend)
gend = generate(counts[1])
discard triangulatePoints[array[2, float32], float32](gend)
for c in counts:
var points = generate(c)
let strt = getMonotime()
discard triangulatePoints[array[2, float32], float32](points)
let elpsd = (getMonotime() - strt).inMilliseconds
writeStyled(align($elpsd & "ms", 6) & " ", {styleDim})
flushFile(stdout)
writeLine(stdout, "")
# bench float64 points
writeLine(stdout, "")
writeStyled(" fromPoints (float64):\n", {styleBright})
for i in 0 ..< distNames.len:
writeStyled(" " & align(distNames[i], 10) & ":", {styleDim})
let generate = f64Distributions[i]
# warmup
var gend = generate(counts[0])
discard triangulatePoints[array[2, float64], float64](gend)
gend = generate(counts[1])
discard triangulatePoints[array[2, float64], float64](gend)
for c in counts:
var points = generate(c)
let strt = getMonotime()
discard triangulatePoints[array[2, float64], float64](points)
let elpsd = (getMonotime() - strt).inMilliseconds
writeStyled(align($elpsd & "ms", 6) & " ", {styleDim})
flushFile(stdout)
writeLine(stdout, "")
# bench float32 coords
writeLine(stdout, "")
writeStyled(" fromCoords (float32):\n", {styleBright})
for i in 0 ..< distNames.len:
writeStyled(" " & align(distNames[i], 10) & ":", {styleDim})
let generate = f32Distributions[i]
var
pointsC0 = generate(counts[0])
coordsC0 = newSeq[float32](pointsC0.len * 2)
pointsC1 = generate(counts[1])
coordsC1 = newSeq[float32](pointsC1.len * 2)
for i, point in pointsC0:
coordsC0[2 * i] = point[0]
coordsC0[2 * i + 1] = point[1]
for i, point in pointsC1:
coordsC1[2 * i] = point[0]
coordsC1[2 * i + 1] = point[1]
# warmup
discard triangulateCoords[float32](coordsC0)
discard triangulateCoords[float32](coordsC1)
for c in counts:
var
points = generate(c)
coords = newSeq[float32](points.len * 2)
for i, point in points:
coords[2 * i] = point[0]
coords[2 * i + 1] = point[1]
let strt = getMonotime()
discard triangulateCoords[float32](coords)
let elpsd = (getMonotime() - strt).inMilliseconds
writeStyled(align($elpsd & "ms", 6) & " ", {styleDim})
flushFile(stdout)
writeLine(stdout, "")
# bench float64 coords
writeLine(stdout, "")
writeStyled(" fromCoords (float64):\n", {styleBright})
for i in 0 ..< distNames.len:
writeStyled(" " & align(distNames[i], 10) & ":", {styleDim})
let generate = f64Distributions[i]
var
pointsC0 = generate(counts[0])
coordsC0 = newSeq[float64](pointsC0.len * 2)
pointsC1 = generate(counts[1])
coordsC1 = newSeq[float64](pointsC1.len * 2)
for i, point in pointsC0:
coordsC0[2 * i] = point[0]
coordsC0[2 * i + 1] = point[1]
for i, point in pointsC1:
coordsC1[2 * i] = point[0]
coordsC1[2 * i + 1] = point[1]
# warmup
discard triangulateCoords[float64](coordsC0)
discard triangulateCoords[float64](coordsC1)
for c in counts:
var
points = generate(c)
coords = newSeq[float64](points.len * 2)
for i, point in points:
coords[2 * i] = point[0]
coords[2 * i + 1] = point[1]
let strt = getMonotime()
discard triangulateCoords[float64](coords)
let elpsd = (getMonotime() - strt).inMilliseconds
writeStyled(align($elpsd & "ms", 6) & " ", {styleDim})
flushFile(stdout)
writeLine(stdout, "")
stdout.resetAttributes()