Skip to content

Commit f51686a

Browse files
authored
refactor(benchmark): extract shared mean stats helper (#4686)
1 parent 5991c6e commit f51686a

1 file changed

Lines changed: 27 additions & 32 deletions

File tree

resources/benchmark/statistics.ts

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,44 @@ export function computeStats(
2424
assert(timingSamples.length > 1);
2525
assert(memorySamples.length > 0);
2626

27-
// Compute the sample mean (estimate of the population mean).
28-
let mean = 0;
29-
for (const clocked of timingSamples) {
30-
mean += clocked;
31-
}
32-
mean /= timingSamples.length;
27+
const { mean, marginOfError } = computeMeanStats(timingSamples);
3328

3429
let meanMemUsed = 0;
3530
for (const memUsed of memorySamples) {
3631
meanMemUsed += memUsed;
3732
}
3833
meanMemUsed /= memorySamples.length;
3934

40-
// Compute the sample variance (estimate of the population variance).
41-
let variance = 0;
42-
for (const clocked of timingSamples) {
43-
variance += (clocked - mean) ** 2;
44-
}
45-
variance /= timingSamples.length - 1;
46-
47-
// Compute the sample standard deviation (estimate of the population standard deviation).
48-
const sd = Math.sqrt(variance);
49-
50-
// Compute the standard error of the mean (a.k.a. the standard deviation of the sampling distribution of the sample mean).
51-
const sem = sd / Math.sqrt(timingSamples.length);
52-
53-
// Compute the degrees of freedom.
54-
const df = timingSamples.length - 1;
55-
56-
// Compute the critical value.
57-
const critical = tTable[df] ?? tTableInfinity;
58-
59-
// Compute the margin of error.
60-
const moe = sem * critical;
61-
62-
// The relative margin of error (expressed as a percentage of the mean).
63-
const rme = (moe / mean) * 100 || 0;
64-
6535
return {
6636
name,
6737
memPerOp: Math.floor(meanMemUsed),
6838
ops: NS_PER_SEC / mean,
69-
deviation: rme,
39+
deviation: (marginOfError / mean) * 100 || 0,
7040
numSamples: timingSamples.length,
7141
};
7242
}
43+
44+
function computeMeanStats(samples: ReadonlyArray<number>): {
45+
mean: number;
46+
marginOfError: number;
47+
} {
48+
assert(samples.length > 1);
49+
50+
let mean = 0;
51+
for (const sample of samples) {
52+
mean += sample;
53+
}
54+
mean /= samples.length;
55+
56+
let variance = 0;
57+
for (const sample of samples) {
58+
variance += (sample - mean) ** 2;
59+
}
60+
variance /= samples.length - 1;
61+
62+
const sd = Math.sqrt(variance);
63+
const sem = sd / Math.sqrt(samples.length);
64+
const df = samples.length - 1;
65+
const critical = tTable[df] ?? tTableInfinity;
66+
return { mean, marginOfError: sem * critical };
67+
}

0 commit comments

Comments
 (0)