Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include thousands-separators in bencher output #705

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
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");
}
}