Skip to content

Commit 8d0f52a

Browse files
committed
Use human readable durations
1 parent e3c6616 commit 8d0f52a

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/write.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,55 @@ function floatStr(n: number) {
148148
return n.toString();
149149
}
150150

151+
const round = (n: number, places: number = 3): number =>
152+
parseFloat(n.toFixed(places));
153+
154+
function strDuration(n: number): string {
155+
let unit = "ns";
156+
if (n >= 6e10) {
157+
let secs = n / 1e9;
158+
const mins = Math.floor(secs / 60);
159+
secs %= 60;
160+
return `${mins}m ${round(secs)}s`
161+
}
162+
163+
if (n >= 1e9) {
164+
unit = "s";
165+
n /= 1e9;
166+
} else if (n >= 1e6) {
167+
unit = "ms";
168+
n /= 1e6;
169+
} else if (n >= 1e3) {
170+
unit = "μs";
171+
n /= 1e3;
172+
}
173+
174+
return `${round(n)}${unit}`;
175+
}
176+
177+
let NS_RANGE_REGEX = /^±\s*([0-9]+)$/;
178+
151179
function strVal(b: BenchmarkResult): string {
152-
let s = `\`${b.value}\` ${b.unit}`;
153-
if (b.range) {
154-
s += ` (\`${b.range}\`)`;
180+
181+
let s = "";
182+
let range = b.range;
183+
184+
if (b.unit == 'ns/iter') {
185+
s += `\`${strDuration(b.value)}/iter\``;
186+
if (range && range.match(NS_RANGE_REGEX)) {
187+
range = "± " + strDuration(parseInt(range.replace(NS_RANGE_REGEX, "$1")));
188+
} else {
189+
// If we can't show a unit, just give up...
190+
range = undefined;
191+
}
192+
} else {
193+
s += `\`${b.value} ${b.unit}\``;
155194
}
195+
196+
if (range) {
197+
s += `<br>(\`${range}\`)`;
198+
}
199+
156200
return s;
157201
}
158202

0 commit comments

Comments
 (0)