• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/H03-May-2022-98

src/H03-May-2022-775600

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

.gitignoreH A D01-Jan-197018 32

.travis.ymlH A D01-Jan-197077 118

Cargo.tomlH A D01-Jan-19701.5 KiB4743

Cargo.toml.orig-cargoH A D01-Jan-1970958 3931

LICENSEH A D01-Jan-19701.3 KiB2620

README.mdH A D01-Jan-19703.3 KiB9265

appveyor.ymlH A D01-Jan-1970833 3631

README.md

1[![Documentation](https://docs.rs/coarsetime/badge.svg)](https://docs.rs/coarsetime)
2[![Build Status](https://travis-ci.org/jedisct1/rust-coarsetime.svg?branch=master)](https://travis-ci.org/jedisct1/rust-coarsetime?branch=master)
3[![Windows build status](https://ci.appveyor.com/api/projects/status/xlbhk9850dvl5ylh?svg=true)](https://ci.appveyor.com/project/jedisct1/rust-coarsetime)
4# coarsetime
5
6A Rust crate to make time measurements, that focuses on speed.
7
8This crate is a partial replacement for the `Time` and `Duration` structures
9from the standard library, with the following differences:
10
11* Speed is privileged over accuracy. In particular, `CLOCK_MONOTONIC_COARSE` is
12used to retrieve the clock value on Linux systems, and transformations avoid
13operations that can be slow on non-Intel systems.
14* The number of system calls can be kept to a minimum. The "most recent
15timestamp" is always kept in memory. It can be read with just a load operation,
16and can be updated only as frequently as necessary.
17
18# Installation
19
20`coarsetime` is available on [crates.io](https://crates.io/crates/coarsetime)
21and works on Rust stable, beta, and nightly.
22
23Windows and Unix-like systems are supported.
24
25Available feature:
26
27* `nightly`: rust compile is rust-nightly - This is required to run
28benchmarks.
29
30# Documentation
31
32[API documentation](https://docs.rs/coarsetime)
33
34# Example
35
36```rust
37extern crate coarsetime;
38
39use coarsetime::{Duration, Instant, Updater};
40
41// Get the current instant. This may require a system call, but it may also
42// be faster than the stdlib equivalent.
43let now = Instant::now();
44
45// Get the latest known instant. This operation is super fast.
46// In this case, the value will be identical to `now`, because we haven't
47// updated the latest known instant yet.
48let ts1 = Instant::recent();
49
50// Update the latest known instant. This may require a system call.
51// Note that a call to `Instant::now()` also updates the stored instant.
52Instant::update();
53
54// Now, we may get a different instant. This call is also super fast.
55let ts2 = Instant::recent();
56
57// Compute the time elapsed between ts2 and ts1.
58let elapsed_ts2_ts1 = ts2.duration_since(ts1);
59
60// Operations such as `+` and `-` between `Instant` and `Duration` are also
61// available.
62let elapsed_ts2_ts1 = ts2 - ts1;
63
64// Returns the time elapsed since ts1.
65// This retrieves the actual current time, and may require a system call.
66let elapsed_since_ts1 = ts1.elapsed();
67
68// Returns the approximate time elapsed since ts1.
69// This uses the latest known instant, and is super fast.
70let elapsed_since_recent = ts1.elapsed_since_recent();
71
72// Instant::update() should be called periodically, for example using an
73// event loop. Alternatively, the crate provides an easy way to spawn a
74// background task that will periodically update the latest known instant.
75// Here, the update will happen every 250ms.
76let updater = Updater::new(250).start().unwrap();
77
78// From now on, Instant::recent() will always return an approximation of the
79// current instant.
80let ts3 = Instant::recent();
81
82// Stop the task.
83updater.stop().unwrap();
84
85// Returns the elapsed time since the UNIX epoch
86let unix_timestamp = Clock::now_since_epoch();
87
88// Returns an approximation of the elapsed time since the UNIX epoch, based on
89// the latest time update
90let unix_timestamp_approx = Clock::recent_since_epoch();
91```
92