Skip to content

Commit

Permalink
feat(time): make time more human readable then it gets big
Browse files Browse the repository at this point in the history
  • Loading branch information
sassman committed Jan 30, 2021
1 parent 5fa7654 commit 2c42c87
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- remove `rand` crate dependency in favor of more lightweight `fastrand` crate
- little change of time unit presentation, to be more human readable for slow systems like my Raspi

## [1.1.5] 2021-01-17
### Added
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::{Duration, Instant};
use figlet_rs::FIGfont;

use crate::statistics::{mean, std_deviation};
use crate::utils::{write_once, Throughput, BUF_SIZE_MB, MAX_CYCLES, TOTAL_SIZE_MB};
use crate::utils::{write_once, HumanReadable, Throughput, BUF_SIZE_MB, MAX_CYCLES, TOTAL_SIZE_MB};

mod statistics;
mod utils;
Expand Down
14 changes: 8 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ impl HumanReadable for Duration {
if self >= &ONE_MIN {
let time = (self.as_secs() / 60) as u128;
let seconds = self.as_secs() - (time * 60) as u64;
return format!("{}m {}s", time, seconds);
return format!("{} m {} {:<4}", time, seconds, "s");
} else if self >= &ONE_SEC {
let unit = "s";
return format!("~{}{}", self.as_secs_f32().round(), unit);
return format!("{:>10} {:<4}", self.as_secs_f32().round(), "s");
}
let time = self.as_millis();
let unit = "ms";

format!("{}{}", time, unit)
format!("{:>10} ms", time)
}
}

Expand Down Expand Up @@ -59,7 +57,11 @@ macro_rules! println_stats {
#[macro_export]
macro_rules! println_time_ms {
($label:expr, $value:expr) => {
println_stats!($label, $value, "ms");
println!(
"{:<36} {}",
$label,
Duration::from_millis($value as u64).as_human_readable()
);
};
}

Expand Down

0 comments on commit 2c42c87

Please sign in to comment.