forked from screepers/screeps-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimple benchmarks.js
41 lines (40 loc) · 1.07 KB
/
Simple benchmarks.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
// warinternal 13 March 2017 at 03:44
/**
* Simple benchmark test with sanity check
*
* Usage: benchmark([
* () => doThing(),
* () => doThingAnotherWay(),
* ]);
*
* Output:
*
* Benchmark results, 1 loop(s):
* Time: 1.345, Avg: 1.345, Function: () => doThing()
* Time: 1.118, Avg: 1.118, Function: () => doThingAnotherWay()
*/
function benchmark(arr, iter = 1) {
var exp,
r,
i,
j,
len = arr.length;
var start, end, used;
var results = _.map(arr, fn => ({ fn: fn.toString(), time: 0, avg: 0 }));
for (j = 0; j < iter; j++) {
for (i = 0; i < len; i++) {
start = Game.cpu.getUsed();
results[i].rtn = arr[i]();
used = Game.cpu.getUsed() - start;
if (i > 0 && results[i].rtn != results[0].rtn)
throw new Error("Results are not the same!");
results[i].time += used;
}
}
console.log(`Benchmark results, ${iter} loop(s): `);
_.each(results, res => {
res.avg = _.round(res.time / iter, 3);
res.time = _.round(res.time, 3);
console.log(`Time: ${res.time}, Avg: ${res.avg}, Function: ${res.fn}`);
});
}