execution_time/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
mod time;
mod traits;
pub use self::{time::*, traits::*};
use std::time::{Duration, Instant};
/// Measures the execution time of a code block.
///
/// This struct provides methods to start a timer and print the elapsed time
/// in a user-friendly format.
pub struct ExecutionTime {
start_time: Instant,
}
impl ExecutionTime {
/// Starts a new stopwatch.
///
/// This function initializes the timer by recording the current time.
///
/// ### Examples
///
/// ```
/// use execution_time::ExecutionTime;
///
/// let timer = ExecutionTime::start();
/// // ... your code here ...
/// timer.print_elapsed_time();
/// ```
pub fn start() -> Self {
Self {
start_time: Instant::now(),
}
}
/// Gets the elapsed time as a `Duration`.
///
/// `Duration` represents a span of time composed of whole seconds
/// and a fractional part represented in nanoseconds.
pub fn get_duration(&self) -> Duration {
self.start_time.elapsed()
}
/// Gets the elapsed time as a `Time` struct.
///
/// This method converts the `Duration` into a `Time` struct, which
/// represents the time in terms of days, hours, minutes, and seconds.
pub fn get_time(&self) -> Time {
self.get_duration().get_time()
}
/// Calculates the time elapsed since the timer was started and formats it as a string.
///
/// This method calculates the elapsed time, formats it into a readable string,
/// and includes both the formatted time and the raw `Duration` for debugging.
pub fn get_elapsed_time(&self) -> String {
let duration: Duration = self.get_duration();
let time: Time = duration.get_time();
format!("{} ({duration:?})", time.format_time())
}
/// Prints the time elapsed since the timer was started to the console.
///
/// This method prints the formatted elapsed time to `stdout`.
pub fn print_elapsed_time(&self) {
println!("Elapsed time: {}", self.get_elapsed_time());
}
}
#[cfg(test)]
mod tests {
use super::*;
type Error = Box<dyn std::error::Error>;
#[test]
fn basic_timing() {
let timer = ExecutionTime::start();
std::thread::sleep(Duration::from_nanos(50));
let elapsed = timer.get_duration();
assert!(elapsed >= Duration::from_nanos(45)); // Allow some margin
}
#[test]
/// `cargo test -- --show-output main`
fn main() -> Result<(), Error> {
let timer = ExecutionTime::start();
let duration = timer.get_duration();
println!("duration: {duration:?}");
let time = duration.get_time();
println!("time: {time:?}");
println!("time: {time:#?}");
Ok(())
}
#[test]
/// `cargo test -- --show-output elapsed_time_more_than_nanosecond`
fn elapsed_time_more_than_nanosecond() -> Result<(), Error> {
let duration = Duration::new(0, 57); // 0_000_000_057
let time = duration.get_time();
let formatted_output = format!("{} ({duration:?})", time.format_time());
println!("duration: {duration:?}");
println!("time: {time:#?}");
println!("formatted_output: {formatted_output}\n");
assert_eq!(
time,
Time {
days: 0,
hours: 0,
minutes: 0,
seconds: 0.000000057,
}
);
assert_eq!(formatted_output, "0.000000057 second (57ns)");
Ok(())
}
#[test]
/// `cargo test -- --show-output elapsed_time_more_than_microsecond`
fn elapsed_time_more_than_microsecond() -> Result<(), Error> {
let duration = Duration::new(0, 80_057); // 0_000_080_057
let time = duration.get_time();
let formatted_output = format!("{} ({duration:?})", time.format_time());
println!("duration: {duration:?}");
println!("time: {time:#?}");
println!("formatted_output: {formatted_output}\n");
assert_eq!(
time,
Time {
days: 0,
hours: 0,
minutes: 0,
seconds: 0.000080057,
}
);
assert_eq!(formatted_output, "0.000080057 second (80.057µs)");
Ok(())
}
#[test]
/// `cargo test -- --show-output elapsed_time_more_than_millisecond`
fn elapsed_time_more_than_millisecond() -> Result<(), Error> {
let duration = Duration::new(0, 15_200_000); // 0_015_200_000
let time = duration.get_time();
let formatted_output = format!("{} ({duration:?})", time.format_time());
println!("duration: {duration:?}");
println!("time: {time:#?}");
println!("formatted_output: {formatted_output}\n");
assert_eq!(
time,
Time {
days: 0,
hours: 0,
minutes: 0,
seconds: 0.015200,
}
);
assert_eq!(formatted_output, "0.015200 second (15.2ms)");
Ok(())
}
#[test]
/// `cargo test -- --show-output elapsed_time_more_than_second`
fn elapsed_time_more_than_second() -> Result<(), Error> {
let duration = Duration::new(5, 80_012_045); // 5_080_012_045
let time = duration.get_time();
let formatted_output = format!("{} ({duration:?})", time.format_time());
println!("duration: {duration:?}");
println!("time: {time:#?}");
println!("formatted_output: {formatted_output}\n");
assert_eq!(
time,
Time {
days: 0,
hours: 0,
minutes: 0,
seconds: 5.080012045,
}
);
assert_eq!(formatted_output, "5.080 seconds (5.080012045s)");
Ok(())
}
#[test]
/// `cargo test -- --show-output elapsed_time_more_than_minute`
fn elapsed_time_more_than_minute() -> Result<(), Error> {
let duration = Duration::new(65, 12_345); // 65_000_012_345
let time = duration.get_time();
let formatted_output = format!("{} ({duration:?})", time.format_time());
println!("duration: {duration:?}");
println!("time: {time:#?}");
println!("formatted_output: {formatted_output}\n");
assert_eq!(
time,
Time {
days: 0,
hours: 0,
minutes: 1,
seconds: 5.000012345,
}
);
assert_eq!(formatted_output, "1 minute, 5.000 seconds (65.000012345s)");
Ok(())
}
#[test]
/// `cargo test -- --show-output elapsed_time_more_than_hour`
fn elapsed_time_more_than_hour() -> Result<(), Error> {
let duration = Duration::new(3700, 56_891_730); // 3700.056891730
let time = duration.get_time();
let formatted_output = format!("{} ({duration:?})", time.format_time());
println!("duration: {duration:?}");
println!("time: {time:#?}");
println!("formatted_output: {formatted_output}\n");
assert_eq!(
time,
Time {
days: 0,
hours: 1,
minutes: 1,
seconds: 40.05689173,
}
);
assert_eq!(
formatted_output,
"1 hour, 1 minute, 40.057 seconds (3700.05689173s)"
);
Ok(())
}
#[test]
/// `cargo test -- --show-output elapsed_time_more_than_day`
fn elapsed_time_more_than_day() -> Result<(), Error> {
let seconds = 86400.0 + 2.0 * 3600.0 + 5.0 * 60.0 + 28.03;
let duration = Duration::from_secs_f64(seconds); // 93928.0300
let time = duration.get_time();
let formatted_output = format!("{} ({duration:?})", time.format_time());
println!("duration: {duration:?}");
println!("time: {time:#?}");
println!("formatted_output: {formatted_output}\n");
assert_eq!(
time,
Time {
days: 1,
hours: 2,
minutes: 5,
seconds: 28.030000,
}
);
assert_eq!(
formatted_output,
"1 day, 2 hours, 5 minutes, 28.030 seconds (93928.03s)"
);
Ok(())
}
}