A Rust crate to make time measurements, that focuses on speed.
This crate is a partial replacement for the Time
and Duration
structures
from the standard library, with the following differences:
CLOCK_MONOTONIC_COARSE
is
used to retrieve the clock value on Linux systems, and transformations avoid
operations that can be slow on non-Intel systems.coarsetime
is available on crates.io
and works on Rust stable, beta, and nightly.
Windows and Unix-like systems are supported.
Available features:
nightly
: optimizes for rust-nightlysierra
: optimizes for macOS Sierra```rust extern crate coarsetime;
use coarsetime::{Duration, Instant, Updater};
// Get the current instant. This may require a system call, but it may also // be faster than the stdlib equivalent. let now = Instant::now();
// Get the latest known instant. This operation is super fast.
// In this case, the value will be identical to now
, because we haven't
// updated the latest known instant yet.
let ts1 = Instant::recent();
// Update the latest known instant. This may require a system call.
// Note that a call to Instant::now()
also updates the stored instant.
Instant::update();
// Now, we may get a different instant. This call is also super fast. let ts2 = Instant::recent();
// Compute the time elapsed between ts2 and ts1. let elapsedts2ts1 = ts2.duration_since(ts1);
// Operations such as +
and -
between Instant
and Duration
are also
// available.
let elapsedts2ts1 = ts2 - ts1;
// Returns the time elapsed since ts1. // This retrieves the actual current time, and may require a system call. let elapsedsincets1 = ts1.elapsed();
// Returns the approximate time elapsed since ts1. // This uses the latest known instant, and is super fast. let elapsedsincerecent = ts1.elapsedsincerecent();
// Instant::update() should be called periodically, for example using an // event loop. Alternatively, the crate provides an easy way to spawn a // background task that will periodically update the latest known instant. // Here, the update will happen every 250ms. let updater = Updater::new(250).start().unwrap();
// From now on, Instant::recent() will always return an approximation of the // current instant. let ts3 = Instant::recent();
// Stop the task. updater.stop().unwrap();
// Returns the elapsed time since the UNIX epoch let unixtimestamp = Clock::nowsince_epoch();
// Returns an approximation of the elapsed time since the UNIX epoch, based on // the latest time update let unixtimestampapprox = Clock::recentsinceepoch(); ```