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

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");

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

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");

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

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.