Skip to content

Commit

Permalink
Use human readable durations
Browse files Browse the repository at this point in the history
  • Loading branch information
414owen committed Feb 10, 2025
1 parent e3c6616 commit 627f3b2
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions src/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,56 @@ function floatStr(n: number) {
return n.toString();
}

const round = (n: number, places: number = 3): string =>
n.toFixed(places).replace(/\.?0+$/, "");

function strDuration(n: number): string {
let unit = "ns";
// I could have DRYd this with some defunctionalization
// and a for loop, but I really don't care enough.
if (n > 1000) {
unit = "μs";
n /= 1000;
}
if (n > 1000) {
unit = "ms";
n /= 1000;
}
if (n > 1000) {
unit = "s";
n /= 1000;
}
if (n > 60) {
const mins = Math.floor(n / 60);
const secs = n % 60;
return `${mins}m ${round(secs)}s`
}
return `${round(n)}${unit}`;
}

let NS_RANGE_REGEX = /^±\s*([0-9])+$/;

function strVal(b: BenchmarkResult): string {
let s = `\`${b.value}\` ${b.unit}`;
if (b.range) {
s += ` (\`${b.range}\`)`;

let s = "";
let range = b.range;

if (b.unit == 'ns/iter') {
s += `\`${strDuration(b.value)}/iter\``;
if (range && range.match(NS_RANGE_REGEX)) {
range = "± " + strDuration(parseInt(range.replace(NS_RANGE_REGEX, "$1")));
} else {
// If we can't show a unit, just give up...
range = undefined;
}
} else {
s += `\`${b.value} ${b.unit}\``;
}

if (range) {
s += `<br>(\`${range}\`)`;
}

return s;
}

Expand Down

0 comments on commit 627f3b2

Please # to comment.