From 6b58daa9cda5b7ff05be3890c5588bd2d58d9f35 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Thu, 10 Mar 2022 16:42:14 +0100 Subject: [PATCH] pfs-time-measure initial commit A library to quickly measure the time for some code. --- .gitignore | 2 ++ Cargo.toml | 8 +++++ src/lib.rs | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d040ef2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "pfs-time-measure" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ef93cbb --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,96 @@ +use std::time::{Duration, Instant}; + +struct Measurement { + start: Instant, + finish: Instant, +} + +pub struct Clock { + name: String, + start: Option, + total_duration: Duration, + measurements: Vec, +} + +impl Clock { + pub fn new(name: String, immediately_start: bool) -> Clock { + let start = if immediately_start { + Some(Instant::now()) + } else { + None + }; + + Clock { + name, + start, + total_duration: Duration::default(), + measurements: Vec::with_capacity(16), + } + } + + pub fn restart(&mut self) { + self.start = Some(Instant::now()); + } + + pub fn stop(&mut self) -> Option { + if !self.is_running() { + return None; + } + + // get duration of this measurement + let finish = Instant::now(); + let duration = finish.duration_since(self.start.unwrap()); + + // add duration to total duration + self.total_duration += duration; + + // add measurement to list of measurements + self.measurements.push(Measurement { + start: self.start.unwrap(), + finish: finish, + }); + + // reset + self.start = None; + + // return duration of this specific measurement + Some(duration) + } + + pub fn stop_and_print(&mut self) { + match self.stop() { + Some(duration) => self.print_duration(duration), + None => println!( + "[CLOCK] {} cannot be stopped because timer wasn't running", + self.name + ), + } + } + + fn is_running(&self) -> bool { + self.start.is_some() + } + + fn print_duration(&self, duration: Duration) { + let name = &self.name; + let duration = duration.as_millis(); + let total_duration = self.total_duration.as_millis(); + println!("[CLOCK] {name} took {duration}ms (total {total_duration}ms)"); + } + + pub fn print_total(&self) { + self.print_duration(self.total_duration); + } + + pub fn print_all_measurements(&self) { + for (idx, measurement) in self.measurements.iter().enumerate() { + println!( + "[CLOCK] {}/{}: {}ms", + self.name, + idx, + measurement.finish.duration_since(measurement.start).as_millis() + ); + } + println!("[CLOCK] {}/total: {}ms", self.name, self.total_duration.as_millis()); + } +}