13 releases

✓ Uses Rust 2018 edition

new 0.1.12 Nov 29, 2019
0.1.11 Apr 22, 2019
0.1.9 Nov 23, 2018
0.1.6 Oct 29, 2017
0.1.3 Nov 19, 2016

#7 in Date and time

Download history 113/week @ 2019-08-16 211/week @ 2019-08-23 154/week @ 2019-08-30 169/week @ 2019-09-06 153/week @ 2019-09-13 545/week @ 2019-09-20 350/week @ 2019-09-27 302/week @ 2019-10-04 275/week @ 2019-10-11 427/week @ 2019-10-18 746/week @ 2019-10-25 342/week @ 2019-11-01 179/week @ 2019-11-08 208/week @ 2019-11-15 159/week @ 2019-11-22

1,332 downloads per month
Used in 2 crates

ISC license

22KB
517 lines

Documentation Build Status Windows build status

coarsetime

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:

  • Speed is privileged over accuracy. In particular, 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.
  • The number of system calls can be kept to a minimum. The "most recent timestamp" is always kept in memory. It can be read with just a load operation, and can be updated only as frequently as necessary.

Installation

coarsetime is available on crates.io and works on Rust stable, beta, and nightly.

Windows and Unix-like systems are supported.

Available feature:

  • nightly: rust compile is rust-nightly - This is required to run benchmarks.

Documentation

API documentation

Example

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 elapsed_ts2_ts1 = ts2.duration_since(ts1);

// Operations such as `+` and `-` between `Instant` and `Duration` are also
// available.
let elapsed_ts2_ts1 = ts2 - ts1;

// Returns the time elapsed since ts1.
// This retrieves the actual current time, and may require a system call.
let elapsed_since_ts1 = ts1.elapsed();

// Returns the approximate time elapsed since ts1.
// This uses the latest known instant, and is super fast.
let elapsed_since_recent = ts1.elapsed_since_recent();

// 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 unix_timestamp = Clock::now_since_epoch();

// Returns an approximation of the elapsed time since the UNIX epoch, based on
// the latest time update
let unix_timestamp_approx = Clock::recent_since_epoch();

Dependencies

~33KB