Skip to content

Commit

Permalink
🔨 Add some more benchmarks (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz authored Jan 9, 2023
1 parent c45912f commit 520cca7
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions perf/benchmark.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ console.info(`Seed : ${SEED}\n`);
const numInts = 100_000;
const numIterations = 1_000;

function builtInNoDistribution(from, to) {
const out = Math.random();
return from + ~~(out * (to - from + 1)); // ~~ is Math.floor
}

function noDistribution(from, to, g) {
const out = g.unsafeNext();
return from + (out % (to - from + 1));
}

function fillBench(bench) {
bench.add('built-in (no distrib)', () => {
const g = libReference[PROF_GEN](SEED);
for (let i = 0; i !== numInts; ++i) {
builtInNoDistribution(0, i, g);
}
});
bench.add('reference (no distrib)', () => {
const g = libReference[PROF_GEN](SEED);
for (let i = 0; i !== numInts; ++i) {
Expand All @@ -44,6 +55,24 @@ function fillBench(bench) {
libReference.unsafeUniformIntDistribution(-0x1_0000_0000, i, g);
}
});
bench.add('reference (uniform small array)', () => {
const g = libReference[PROF_GEN](SEED);
for (let i = 0; i !== numInts; ++i) {
libReference.unsafeUniformArrayIntDistribution({ sign: 1, data: [0] }, { sign: 1, data: [i] }, g);
}
});
bench.add('reference (uniform large array)', () => {
const g = libReference[PROF_GEN](SEED);
for (let i = 0; i !== numInts; ++i) {
libReference.unsafeUniformIntDistribution({ sign: -1, data: [1, 0] }, { sign: 1, data: [0, i] }, g);
}
});
bench.add('reference (jump)', () => {
const g = libReference[PROF_GEN](SEED);
for (let i = 0; i !== numInts; ++i) {
g.unsafeJump();
}
});
bench.add('test (no distrib)', () => {
const g = libTest[PROF_GEN](SEED);
for (let i = 0; i !== numInts; ++i) {
Expand All @@ -62,6 +91,24 @@ function fillBench(bench) {
libTest.unsafeUniformIntDistribution(-0x1_0000_0000, i, g);
}
});
bench.add('test (uniform small array)', () => {
const g = libTest[PROF_GEN](SEED);
for (let i = 0; i !== numInts; ++i) {
libTest.unsafeUniformArrayIntDistribution({ sign: 1, data: [0] }, { sign: 1, data: [i] }, g);
}
});
bench.add('test (uniform large array)', () => {
const g = libTest[PROF_GEN](SEED);
for (let i = 0; i !== numInts; ++i) {
libTest.unsafeUniformIntDistribution({ sign: -1, data: [1, 0] }, { sign: 1, data: [0, i] }, g);
}
});
bench.add('test (jump)', () => {
const g = libTest[PROF_GEN](SEED);
for (let i = 0; i !== numInts; ++i) {
g.unsafeJump();
}
});
}

async function runner() {
Expand Down

0 comments on commit 520cca7

Please sign in to comment.