-
Notifications
You must be signed in to change notification settings - Fork 6
/
bitbench.ts
70 lines (61 loc) · 1.97 KB
/
bitbench.ts
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
import type {BitSet} from './BitSet';
import {BitSet8, BitSet16, BitSet32} from './BitSet';
import {fmtTime, summarizeTimes} from './bench';
function prepare(bs: BitSet, size: number){
for(let i = 0; i < size; i++){
if(Math.random() > 0.5){
bs.add(i);
}
}
return bs;
}
function benchCB(bs: BitSet, iter: number){
const times = new Array();
for(let i = 0; i < iter; i++){
const start = process.hrtime.bigint();
let count = 0;
bs.forEach(idx => count += idx%2);
const end = process.hrtime.bigint();
times.push(end - start);
}
return times;
}
function cloneInto(a: BitSet, b: BitSet){
a.forEach(idx => b.add(idx));
return b;
}
async function main(args: string[]){
const size = args[0] ? +args[0] : 4096;
const ITER = args[1] ? +args[1] : 10;
const bs8 = prepare(new BitSet8(size), size);
const bs16 = cloneInto(bs8, new BitSet16(size));
const bs32 = cloneInto(bs8, new BitSet32(size));
console.log('size:', size, 'iter:', ITER);
const times8 = benchCB(bs8, ITER);
console.table(summarizeTimes(times8));
const times16 = benchCB(bs16, ITER);
console.table(summarizeTimes(times16));
const times32 = benchCB(bs32, ITER);
console.table(summarizeTimes(times32));
/*
console.log('callback');
for(let i = 0; i < ITER; i++){
const start = process.hrtime.bigint();
let count = 0;
bs8.forEach(idx => count += idx%2);
const end = process.hrtime.bigint();
console.log('count:', count, 'time:', fmtTime(end - start));
}
console.log('iterator');
for(let i = 0; i < ITER; i++){
const start = process.hrtime.bigint();
let count = 0;
for(const idx of bs8){
count += idx%2;
}
const end = process.hrtime.bigint();
console.log('count:', count, 'time:', fmtTime(end - start));
}
*/
}
main(process.argv.slice(2)).catch(ex => { throw ex; });