-
Notifications
You must be signed in to change notification settings - Fork 3
/
bench.ts
29 lines (24 loc) · 873 Bytes
/
bench.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
import {forwardmask, backwardmask, fastpopcnt} from "./index";
console.log('Popcnt benchmark')
// O(bits)
function popcnt(v: bigint) {
let c = 0
for (; v; c++) {
v &= v - 1n; // clear the least significant bit set
}
return c
}
const samples = 100000
const sample = (1n << 2048n)-1n
let res = 0
let start = Date.now()
for (let i = 0; i < samples; i++) res = popcnt(sample)
let time = Date.now() - start
console.log(`popcnt: ${res}, samples: ${samples}, ${time}ms, ${samples / time}p/ms`)
start = Date.now()
const max_mask = 8192n
const _popcnt = fastpopcnt(max_mask)
for (let i = 0; i < samples; i++) res = Number(_popcnt(sample))
time = Date.now() - start
console.log(`fastpopcnt(${max_mask}b): ${res}, samples: ${samples}, ${time}ms, ${samples / time}p/ms`)
//console.log(forwardmask(64n).map(x => x.toString(2) + ' ' + x.toString(2).length))