Skip to content

Commit 6992a91

Browse files
committed
Use human readable durations
1 parent e3c6616 commit 6992a91

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

src/write.ts

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

151+
const round = (n: number, places: number = 3): string =>
152+
n.toFixed(places).replace(/\.?0+$/, "");
153+
154+
function strDuration(n: number): string {
155+
let unit = "ns";
156+
// I could have DRYd this with some defunctionalization
157+
// and a for loop, but I really don't care enough.
158+
if (n > 1000) {
159+
unit = "μs";
160+
n /= 1000;
161+
if (n > 1000) {
162+
unit = "ms";
163+
n /= 1000;
164+
if (n > 1000) {
165+
unit = "s";
166+
n /= 1000;
167+
if (n > 60) {
168+
const mins = Math.floor(n / 60);
169+
const secs = n % 60;
170+
return `${mins}m ${round(secs)}s`
171+
}
172+
}
173+
}
174+
}
175+
return `${round(n)}${unit}`;
176+
}
177+
178+
let NS_RANGE_REGEX = /^±\s*([0-9])+$/;
179+
151180
function strVal(b: BenchmarkResult): string {
152-
let s = `\`${b.value}\` ${b.unit}`;
153-
if (b.range) {
154-
s += ` (\`${b.range}\`)`;
181+
182+
let s = "";
183+
let range = b.range;
184+
185+
if (b.unit == 'ns/iter') {
186+
s += `\`${strDuration(b.value)}/iter\``;
187+
if (range && range.match(NS_RANGE_REGEX)) {
188+
range = "± " + strDuration(parseInt(range.replace(NS_RANGE_REGEX, "$1")));
189+
} else {
190+
// If we can't show a unit, just give up...
191+
range = undefined;
192+
}
193+
} else {
194+
s += `\`${b.value} ${b.unit}\``;
155195
}
196+
197+
if (range) {
198+
s += `<br>(\`${range}\`)`;
199+
}
200+
156201
return s;
157202
}
158203

0 commit comments

Comments
 (0)