pub struct Stopwatch {
pub elapsed: Duration,
pub timer: Instant,
pub is_running: bool,
}Expand description
A simple stopwatch implementation.
§Usage
use ticky::Stopwatch;
let mut sw = Stopwatch::start_new();
// Do something …
sw.stop();
println!("Elapsed time: {}ms", sw.elapsed_ms_whole());Fields§
§elapsed: DurationThe total elapsed time.
timer: InstantThe time at which the stopwatch was last started.
is_running: boolWhether the stopwatch is currently running.
Implementations§
Source§impl Stopwatch
impl Stopwatch
Sourcepub fn start(&mut self)
pub fn start(&mut self)
Starts (or resumes) the stopwatch.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch (pausing it)
assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 100); // Allow for some error (± 100 milliseconds)
sw.start(); // Start the stopwatch again (resuming it)
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed_ms_whole().abs_diff(2_000) < 100); // Allow for some error (± 100 milliseconds)Sourcepub fn stop(&mut self)
pub fn stop(&mut self)
Stops (or pauses) the stopwatch.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch (pausing it)
assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 100); // Allow for some error (± 100 milliseconds)
sw.start(); // Start the stopwatch again (resuming it)
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed_ms_whole().abs_diff(2_000) < 100); // Allow for some error (± 100 milliseconds)Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the total elapsed time.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed().as_millis().abs_diff(1_000) < 100); // Allow for some error (± 100 milliseconds)Sourcepub fn elapsed_ms_whole(&mut self) -> u128
pub fn elapsed_ms_whole(&mut self) -> u128
Returns the total elapsed time in milliseconds.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 100); // Allow for some error (± 100 milliseconds)Sourcepub fn elapsed_us_whole(&mut self) -> u128
pub fn elapsed_us_whole(&mut self) -> u128
Returns the total elapsed time in microseconds.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_micros(1_000_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed_us_whole().abs_diff(1_000_000) < 10_000); // Allow for some error (± 10,000 microseconds)Sourcepub fn elapsed_ns_whole(&mut self) -> u128
pub fn elapsed_ns_whole(&mut self) -> u128
Returns the total elapsed time in nanoseconds.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_nanos(1_000_000_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed_ns_whole().abs_diff(1_000_000_000) < 100_000_000); // Allow for some error (± 100,000,000 nanoseconds)Sourcepub fn elapsed_s(&mut self) -> f64
pub fn elapsed_s(&mut self) -> f64
Returns the total elapsed time in fractional seconds.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_millis(1_500)); // Wait for 1.5 seconds
sw.stop(); // Stop the stopwatch
assert!((sw.elapsed_s() - 1.5).abs() < 0.01); // Allow for some error (± 0.01 seconds)Sourcepub fn elapsed_s_whole(&self) -> u64
pub fn elapsed_s_whole(&self) -> u64
Returns the total elapsed time in whole seconds.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_millis(1_500)); // Wait for 1.5 seconds
sw.stop(); // Stop the stopwatch
assert_eq!(sw.elapsed_s_whole(), 1);Source§impl Stopwatch
impl Stopwatch
Sourcepub fn new() -> Stopwatch
pub fn new() -> Stopwatch
Creates a new stopwatch.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)Sourcepub fn start_new() -> Stopwatch
pub fn start_new() -> Stopwatch
Creates a new stopwatch and starts it.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::start_new(); // Create a new stopwatch, and start it
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the stopwatch.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)
sw.reset(); // Reset the stopwatch
assert!(sw.elapsed_ms_whole().abs_diff(0) < 10); // Allow for some error (± 10 milliseconds)
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)Sourcepub fn restart(&mut self)
pub fn restart(&mut self)
Resets and starts the stopwatch.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
sw.start(); // Start the stopwatch
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)
sw.restart(); // Reset and start the stopwatch
std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
sw.stop(); // Stop the stopwatch
assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)Sourcepub fn is_running(&mut self) -> bool
pub fn is_running(&mut self) -> bool
Returns true if the stopwatch is running, and false if not.
§Example
use ticky::Stopwatch;
let mut sw = Stopwatch::new(); // Create a new stopwatch
assert_eq!(sw.is_running(), false); // The stopwatch is not running
sw.start(); // Start the stopwatch
assert_eq!(sw.is_running(), true); // The stopwatch is running
sw.stop(); // Stop the stopwatch
assert_eq!(sw.is_running(), false); // The stopwatch is not runningTrait Implementations§
Source§impl Ord for Stopwatch
impl Ord for Stopwatch
Source§impl PartialOrd for Stopwatch
impl PartialOrd for Stopwatch
impl Copy for Stopwatch
impl Eq for Stopwatch
impl StructuralPartialEq for Stopwatch
Auto Trait Implementations§
impl Freeze for Stopwatch
impl RefUnwindSafe for Stopwatch
impl Send for Stopwatch
impl Sync for Stopwatch
impl Unpin for Stopwatch
impl UnwindSafe for Stopwatch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more