Struct russell_lab::Stopwatch
source · [−]pub struct Stopwatch { /* private fields */ }
Expand description
Assists in measuring computation time
Examples
Printing elapsed times
use russell_lab::Stopwatch;
use std::thread::sleep;
use std::time::Duration;
fn expensive_calculation() {
sleep(Duration::new(0, 1_000));
}
let mut sw = Stopwatch::new("current dt = ");
expensive_calculation();
sw.stop();
println!("{}", sw);
sw.reset();
expensive_calculation();
sw.stop();
println!("{}", sw);
sw.reset();
expensive_calculation();
sw.stop();
println!("{}", sw);
Recording elapsed times
use russell_lab::Stopwatch;
use std::thread::sleep;
use std::time::Duration;
fn expensive_calculation() {
sleep(Duration::new(0, 1_000));
}
let mut elapsed_times = vec![0_u128; 3];
let mut sw = Stopwatch::new("");
expensive_calculation();
elapsed_times[0] = sw.stop_and_reset();
expensive_calculation();
elapsed_times[1] = sw.stop_and_reset();
expensive_calculation();
elapsed_times[2] = sw.stop_and_reset();
// println!("{:?}", elapsed_times); // will show something like:
// [57148, 55991, 55299]
Implementations
sourceimpl Stopwatch
impl Stopwatch
sourcepub fn new(label: &'static str) -> Self
pub fn new(label: &'static str) -> Self
Creates and starts a new Stopwatch
Input
label
– used when displaying the elapsed time
Note
The method stop
(or stop_and_reset
) must be called to measure the elapsed time. Until then, the displayed elapsed time is zero, even though the stopwatch has already started.
Example
use russell_lab::Stopwatch;
let sw = Stopwatch::new("elapsed time = ");
assert_eq!(format!("{}", sw), "elapsed time = 0ns");
sourcepub fn stop(&mut self) -> u128
pub fn stop(&mut self) -> u128
Stops the stopwatch and returns the elapsed time
Output
Returns the elapsed time
Example
use russell_lab::Stopwatch;
use std::thread::sleep;
use std::time::Duration;
let mut sw = Stopwatch::new("");
sleep(Duration::new(0, 1_000));
let elapsed = sw.stop();
assert!(elapsed > 0);
// println!("{}", sw); // will show something like:
// 63.099µs
sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the stopwatch to zero elapsed time
Example
use russell_lab::Stopwatch;
use std::thread::sleep;
use std::time::Duration;
let mut sw = Stopwatch::new("delta_t = ");
sleep(Duration::new(0, 1_000));
sw.stop();
sw.reset();
assert_eq!(format!("{}", sw), "delta_t = 0ns");
sourcepub fn stop_and_reset(&mut self) -> u128
pub fn stop_and_reset(&mut self) -> u128
Stops the stopwatch and resets it to zero elapsed time
Output
Returns the elapsed time
Example
use russell_lab::Stopwatch;
use std::thread::sleep;
use std::time::Duration;
let mut sw = Stopwatch::new("current = ");
sleep(Duration::new(0, 1_000));
let elapsed = sw.stop_and_reset();
// println!("{}", format_nanoseconds(elapsed)); // will show something like
// current = 63.099µs
assert!(elapsed > 0);
assert_eq!(format!("{}", sw), "current = 0ns");
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Stopwatch
impl Send for Stopwatch
impl Sync for Stopwatch
impl Unpin for Stopwatch
impl UnwindSafe for Stopwatch
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more