Crate libsw

source ·
Expand description

libsw is a comprehensive stopwatch implementation.

It offers checked stopping and arithmetic, precise control over when operations occur, and supports arbitrary timekeeping types.

Example

The following (contrived) example shows the basic features of the crate. You are encouraged to read the examples provided for methods of StopwatchImpl and Guard for more complex use cases.

If you want to do benchmarking, please use something like Criterion.

use libsw::{Guard, Sw};

use core::time::Duration;
use std::thread;
use std::time::Instant;

fn main() {
    if let Err(err) = try_main() {
        // libsw::Error implements display, this
        // will explain the error
        eprintln!("error: {err}");
    }
}

fn try_main() -> libsw::Result<()> {
    let mut sw = Sw::new();

    // time how long `expensive` takes
    sw.start()?;
    let x = expensive();
    sw.stop()?;

    println!(
        "expensive function returned {x} after {:?}",
        sw.elapsed()
    );

    sw.reset();

    // another way to do this is with guards. the
    // guard will keep `sw` running until it's
    // dropped.
    let y = expensive_timed(sw.guard()?);
    println!(
        "same function returned {y} after {:?}",
        sw.elapsed()
    );

    // uh-oh, this will fail! the stopwatch is already stopped.
    sw.stop()?;

    Ok(())
}

fn expensive() -> u32 {
    thread::sleep(Duration::from_millis(100));
    1
}

fn expensive_timed<I: libsw::Instant>(_guard: Guard<'_, I>) -> u32 {
    // guard is dropped when the function returns,
    // automatically stopping the guarded
    // stopwatch
    expensive()
}

Features

NameFeatures enabledDescription
defaultstd_instant, std_systemtimeEnabled by default.
stdDepends on the standard library. Implements std::error::Error for Error.
nightlyImplements core::error::Error for Error if std is not enabled. Requires a nightly compiler.
std_instantstdImplements Instant for std::time::Instant. Exposes Sw type alias.
std_systemtimestdImplements Instant for std::time::SystemTime. Exposes SystemSw type alias.
tokiostdImplements Instant for tokio::time::Instant. Exposes TokioSw type alias.
timestdImplements Instant for time::Instant. Exposes TimeSw type alias. Bumps MSRV to 1.62.1.
coarsetimestdImplements Instant for coarsetime::Instant. Exposes CoarseSw type alias.
quantastdImplements Instant for quanta::Instant. Exposes QuantaSw type alias.

Timekeeping support

libsw can be used with any timekeeping type that implements Instant, as long as the appropriate feature flag is enabled.

See Instant’s documentation for a list of types supported out of the box.

no_std support

The std feature flag unsets #[no_std]. It is enabled by default, but you can disable it by disabling the default features.

In Cargo.toml,

[dependencies]
# replace '...' with the appropriate version
libsw = { version = ..., default-features = false }

Compiler support

The minimum supported version of Rust is 1.61.0.

Safety

libsw contains no unsafe code (#![forbid(unsafe_code)]).

Structs

  • A running, guarded, stopwatch. When dropped, the stopwatch will automatically stop.
  • A stopwatch measures and accumulates elapsed time between starts and stops.

Enums

  • Enumeration over possible errors.

Traits

  • A trait outlining the behavior of a timekeeping type.

Type Definitions