Skip to content

Commit

Permalink
Include thousands-separators in bencher output (#705)
Browse files Browse the repository at this point in the history
As mentioned in #704, the output generated when using the `bencher`
output format does not quite match up with that of libtest. One reason
being that thousands-separators are not emitted.
With this change we adjust the formatting logic for integers (which is
only used in `bencher` style reports) to emit thousands-separators, as
libtest does.

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o committed Aug 10, 2023
1 parent 7feeb35 commit daa84ca
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/format.rs
Expand Up @@ -72,8 +72,35 @@ pub fn iter_count(iterations: u64) -> String {
}
}

/// Format a number with thousands separators.
// Based on the corresponding libtest functionality, see
// https://github.com/rust-lang/rust/blob/557359f92512ca88b62a602ebda291f17a953002/library/test/src/bench.rs#L87-L109
fn thousands_sep(mut n: u64, sep: char) -> String {
use std::fmt::Write;
let mut output = String::new();
let mut trailing = false;
for &pow in &[9, 6, 3, 0] {
let base = 10_u64.pow(pow);
if pow == 0 || trailing || n / base != 0 {
if !trailing {
write!(output, "{}", n / base).unwrap();
} else {
write!(output, "{:03}", n / base).unwrap();
}
if pow != 0 {
output.push(sep);
}
trailing = true;
}
n %= base;
}

output
}

/// Format a value as an integer, including thousands-separators.
pub fn integer(n: f64) -> String {
format!("{}", n as u64)
thousands_sep(n as u64, ',')
}

#[cfg(test)]
Expand Down Expand Up @@ -101,4 +128,10 @@ mod test {
float *= 2.0;
}
}

#[test]
fn integer_thousands_sep() {
let n = 140352319.0;
assert_eq!(integer(n), "140,352,319");
}
}

0 comments on commit daa84ca

Please sign in to comment.