-
Notifications
You must be signed in to change notification settings - Fork 68
/
bench.js
315 lines (275 loc) · 8.25 KB
/
bench.js
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
S = typeof S === 'undefined' ? require('..') : S;
var now = typeof process === 'undefined' ? browserNow : nodeNow;
var COUNT = 1e5;
main();
function main() {
var createTotal = 0;
createTotal += bench(createDataSignals, COUNT, COUNT);
createTotal += bench(createComputations0to1, COUNT, 0);
createTotal += bench(createComputations1to1, COUNT, COUNT);
createTotal += bench(createComputations2to1, COUNT / 2, COUNT);
createTotal += bench(createComputations4to1, COUNT / 4, COUNT);
createTotal += bench(createComputations1000to1, COUNT / 1000, COUNT);
//total += bench1(createComputations8, COUNT, 8 * COUNT);
createTotal += bench(createComputations1to2, COUNT, COUNT / 2);
createTotal += bench(createComputations1to4, COUNT, COUNT / 4);
createTotal += bench(createComputations1to8, COUNT, COUNT / 8);
createTotal += bench(createComputations1to1000, COUNT, COUNT / 1000);
console.log(`create total: ${createTotal.toFixed(0)}`);
console.log('---');
var updateTotal = 0;
updateTotal += bench(updateComputations1to1, COUNT * 4, 1);
updateTotal += bench(updateComputations2to1, COUNT * 2, 2);
updateTotal += bench(updateComputations4to1, COUNT, 4);
updateTotal += bench(updateComputations1000to1, COUNT / 100, 1000);
updateTotal += bench(updateComputations1to2, COUNT * 4, 1);
updateTotal += bench(updateComputations1to4, COUNT * 4, 1);
updateTotal += bench(updateComputations1to1000, COUNT * 4, 1);
console.log(`update total: ${updateTotal.toFixed(0)}`);
console.log(`total: ${(createTotal + updateTotal).toFixed(0)}`);
}
function bench(fn, count, scount) {
var time = run(fn, count, scount);
console.log(`${fn.name}: ${time.toFixed(0)}`);
return time;
}
function run(fn, n, scount) {
// prep n * arity sources
var start,
end;
S.root(function () {
// run 3 times to warm up
var sources = createDataSignals(scount, []);
fn(n / 100, sources);
sources = createDataSignals(scount, []);
fn(n / 100, sources);
sources = createDataSignals(scount, []);
%OptimizeFunctionOnNextCall(fn);
fn(n / 100, sources);
sources = createDataSignals(scount, []);
for (var i = 0; i < scount; i++) {
sources[i]();
sources[i]();
//%OptimizeFunctionOnNextCall(sources[i]);
sources[i]();
}
// start GC clean
%CollectGarbage(null);
start = now();
fn(n, sources);
// end GC clean
sources = null;
%CollectGarbage(null);
end = now();
});
return end - start;
}
function createDataSignals(n, sources) {
for (var i = 0; i < n; i++) {
sources[i] = S.data(i);
}
return sources;
}
function createComputations0to1(n, sources) {
for (var i = 0; i < n; i++) {
createComputation0(i);
}
}
function createComputations1to1000(n, sources) {
for (var i = 0; i < n / 1000; i++) {
for (var j = 0; j < 1000; j++) {
createComputation1(sources[i]);
}
//sources[i] = null;
}
}
function createComputations1to8(n, sources) {
for (var i = 0; i < n / 8; i++) {
createComputation1(sources[i]);
createComputation1(sources[i]);
createComputation1(sources[i]);
createComputation1(sources[i]);
createComputation1(sources[i]);
createComputation1(sources[i]);
createComputation1(sources[i]);
createComputation1(sources[i]);
//sources[i] = null;
}
}
function createComputations1to4(n, sources) {
for (var i = 0; i < n / 4; i++) {
createComputation1(sources[i]);
createComputation1(sources[i]);
createComputation1(sources[i]);
createComputation1(sources[i]);
//sources[i] = null;
}
}
function createComputations1to2(n, sources) {
for (var i = 0; i < n / 2; i++) {
createComputation1(sources[i]);
createComputation1(sources[i]);
//sources[i] = null;
}
}
function createComputations1to1(n, sources) {
for (var i = 0; i < n; i++) {
createComputation1(sources[i]);
//sources[i] = null;
}
}
function createComputations2to1(n, sources) {
for (var i = 0; i < n; i++) {
createComputation2(
sources[i * 2],
sources[i * 2 + 1]
);
//sources[i * 2] = null;
//sources[i * 2 + 1] = null;
}
}
function createComputations4to1(n, sources) {
for (var i = 0; i < n; i++) {
createComputation4(
sources[i * 4],
sources[i * 4 + 1],
sources[i * 4 + 2],
sources[i * 4 + 3]
);
//sources[i * 4] = null;
//sources[i * 4 + 1] = null;
//sources[i * 4 + 2] = null;
//sources[i * 4 + 3] = null;
}
}
function createComputations8(n, sources) {
for (var i = 0; i < n; i++) {
createComputation8(
sources[i * 8],
sources[i * 8 + 1],
sources[i * 8 + 2],
sources[i * 8 + 3],
sources[i * 8 + 4],
sources[i * 8 + 5],
sources[i * 8 + 6],
sources[i * 8 + 7]
);
sources[i * 8] = null;
sources[i * 8 + 1] = null;
sources[i * 8 + 2] = null;
sources[i * 8 + 3] = null;
sources[i * 8 + 4] = null;
sources[i * 8 + 5] = null;
sources[i * 8 + 6] = null;
sources[i * 8 + 7] = null;
}
}
// only create n / 100 computations, as otherwise takes too long
function createComputations1000to1(n, sources) {
for (var i = 0; i < n; i++) {
createComputation1000(sources, i * 1000);
}
}
function createComputation0(i) {
S(function () { return i; });
}
function createComputation1(s1) {
S(function () { return s1(); });
}
function createComputation2(s1, s2) {
S(function () { return s1() + s2(); });
}
function createComputation4(s1, s2, s3, s4) {
S(function () { return s1() + s2() + s3() + s4(); });
}
function createComputation8(s1, s2, s3, s4, s5, s6, s7, s8) {
S(function () { return s1() + s2() + s3() + s4() + s5() + s6() + s7() + s8(); });
}
function createComputation1000(ss, offset) {
S(function () {
var sum = 0;
for (var i = 0; i < 1000; i++) {
sum += ss[offset + i]();
}
return sum;
});
}
function updateComputations1to1(n, sources) {
var s1 = sources[0],
c = S(function () { return s1(); });
for (var i = 0; i < n; i++) {
s1(i);
}
}
function updateComputations2to1(n, sources) {
var s1 = sources[0],
s2 = sources[1],
c = S(function () { return s1() + s2(); });
for (var i = 0; i < n; i++) {
s1(i);
}
}
function updateComputations4to1(n, sources) {
var s1 = sources[0],
s2 = sources[1],
s3 = sources[2],
s4 = sources[3],
c = S(function () { return s1() + s2() + s3() + s4(); });
for (var i = 0; i < n; i++) {
s1(i);
}
}
function updateComputations1000to1(n, sources) {
var s1 = sources[0],
c = S(function () {
var sum = 0;
for (var i = 0; i < 1000; i++) {
sum += sources[i]();
}
return sum;
});
for (var i = 0; i < n; i++) {
s1(i);
}
}
function updateComputations1to2(n, sources) {
var s1 = sources[0],
c1 = S(function () { return s1(); }),
c2 = S(function () { return s1(); });
for (var i = 0; i < n / 2; i++) {
s1(i);
}
}
function updateComputations1to4(n, sources) {
var s1 = sources[0],
c1 = S(function () { return s1(); }),
c2 = S(function () { return s1(); }),
c3 = S(function () { return s1(); }),
c4 = S(function () { return s1(); });
for (var i = 0; i < n / 4; i++) {
s1(i);
}
}
function updateComputations1to1000(n, sources) {
var s1 = sources[0];
for (var i = 0; i < 1000; i++) {
S(function () { return s1(); });
}
for (var i = 0; i < n / 1000; i++) {
s1(i);
}
}
function browserNow() {
return performance.now();
}
function nodeNow() {
var hrt = process.hrtime();
return hrt[0] * 1000 + hrt[1] / 1e6;
}
function repeat(n, val) {
var arr = [];
for (var i = 0; i < n; i++) {
arr[i] = val;
}
return arr;
}