-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjs-for-range-benchmark.html
247 lines (234 loc) · 4.97 KB
/
js-for-range-benchmark.html
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<br>Width (default 1000): <input id=e_w type=text>
<br>Height (default 1000): <input id=e_h type=text>
<br>Points (default 100): <input id=e_points type=text>
<br>Reps (default 10): <input id=e_reps type=text>
<br><input type=button id=e_run value=Run>
<br><pre id=e_output></pre>
<script>
/// `for (const i of range(3))` => 0, 1, 2
/// `for (const i of range(1,4))` => 1, 2, 3
/// Don't use `i in range(n)`, it will quietly not work.
function* range(a, b) {
if (b === undefined) {
b = a;
a = 0;
}
for (let i = a; i < b; i++) {
yield i;
}
}
function range2(a) {
let i = 0;
return {
next: () => {
return {
done: (i >= a),
value: i++,
};
},
[Symbol.iterator]: function() { return this; },
};
}
function range3(a) {
let i = 0;
const ret = {};
return {
next: () => {
ret.done = (i >= a);
ret.value = i++;
return ret;
},
[Symbol.iterator]: function() { return this; },
};
}
function range4(a) {
return Array(a).keys();
}
/// Prefer `call(() => { ... })` to `(() => { ... })()`\
/// This way, it's clear up-front that we're calling not just defining.
function call(fn) {
return fn();
}
function sleepPromise(ms) {
return new Promise(go => {
setTimeout(go, ms);
});
}
// -
function bench(desc, name, fn) {
const MAX_VAL_PER_POINT = 10;
const data = new Uint32Array(desc.w * desc.h);
let sum = 0;
for (const i of range(desc.points)) {
const val = (Math.random()*MAX_VAL_PER_POINT)|0;
const pos = (Math.random()*data.length)|0;
data[pos] = val;
sum += val;
}
const start = performance.now();
for (const i of range(desc.reps)) {
const found = fn(desc.w, desc.h, data);
console.assert(found == sum, {found}, {sum});
}
const ms = performance.now() - start;
const msPerRep = ms / desc.reps;
e_output.textContent += `\n ${name}: ${msPerRep.toPrecision(3)}ms/rep`;
}
const FLAVORS = {
vanilla_1d: (w, h, data) => {
let sum = 0;
for (let i = 0; i < data.length; i++) {
const val = data[i];
if (val) {
sum += val;
}
}
return sum;
},
vanilla_2d: (w, h, data) => {
let sum = 0;
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
const val = data[y*w+x];
if (val) {
sum += val;
}
}
}
return sum;
},
range_1d: (w, h, data) => {
let sum = 0;
for (const i of range(data.length)) {
const val = data[i];
if (val) {
sum += val;
}
}
return sum;
},
range_2d: (w, h, data) => {
let sum = 0;
for (const y of range(h)) {
for (const x of range(w)) {
const val = data[y*w+x];
if (val) {
sum += val;
}
}
}
return sum;
},
range2_1d: (w, h, data) => {
let sum = 0;
for (const i of range2(data.length)) {
const val = data[i];
if (val) {
sum += val;
}
}
return sum;
},
range2_2d: (w, h, data) => {
let sum = 0;
for (const y of range2(h)) {
for (const x of range2(w)) {
const val = data[y*w+x];
if (val) {
sum += val;
}
}
}
return sum;
},
range3_1d: (w, h, data) => {
let sum = 0;
for (const i of range3(data.length)) {
const val = data[i];
if (val) {
sum += val;
}
}
return sum;
},
range3_2d: (w, h, data) => {
let sum = 0;
for (const y of range3(h)) {
for (const x of range3(w)) {
const val = data[y*w+x];
if (val) {
sum += val;
}
}
}
return sum;
},
range4_1d: (w, h, data) => {
let sum = 0;
for (const i of range4(data.length)) {
const val = data[i];
if (val) {
sum += val;
}
}
return sum;
},
range4_2d: (w, h, data) => {
let sum = 0;
for (const y of range4(h)) {
for (const x of range4(w)) {
const val = data[y*w+x];
if (val) {
sum += val;
}
}
}
return sum;
},
foreach_1d: (w, h, data) => {
let sum = 0;
new Uint8Array(data.length).forEach((_,i) => {
const val = data[i];
if (val) {
sum += val;
}
});
return sum;
},
foreach_2d: (w, h, data) => {
let sum = 0;
new Uint8Array(h).forEach((_,y) => {
new Uint8Array(w).forEach((_,x) => {
const val = data[y*w+x];
if (val) {
sum += val;
}
});
});
return sum;
},
};
async function run() {
const desc = {
w: parseInt(e_w.value) || 1000,
h: parseInt(e_h.value) || 1000,
points: parseInt(e_points.value) || 100,
reps: parseInt(e_reps.value) || 10,
};
e_output.textContent += `\n\n${JSON.stringify(desc)}:`;
for (const [name,fn] of Object.entries(FLAVORS)) {
bench(desc, name, fn);
await sleepPromise();
}
}
run();
e_run.addEventListener('click', run);
</script>
</body>
</html>