#timing #nanosecond #clock #tsc #high-speed #measure-time #rdtsc

minstant

A drop-in replacement for std::time::Instant that measures time with high performance and high accuracy powered by TSC

8 releases

0.1.7 Feb 24, 2024
0.1.6 Jan 24, 2024
0.1.4 Oct 20, 2023
0.1.3 Aug 29, 2023
0.1.1 Jan 6, 2022

#112 in Operating systems

Download history 19845/week @ 2024-01-22 20939/week @ 2024-01-29 7196/week @ 2024-02-05 6043/week @ 2024-02-12 8473/week @ 2024-02-19 10460/week @ 2024-02-26 7907/week @ 2024-03-04 10795/week @ 2024-03-11 10658/week @ 2024-03-18 10729/week @ 2024-03-25 7521/week @ 2024-04-01 13058/week @ 2024-04-08 10492/week @ 2024-04-15 12039/week @ 2024-04-22 7058/week @ 2024-04-29 11076/week @ 2024-05-06

40,951 downloads per month
Used in 35 crates (18 directly)

MIT license

120KB
397 lines

minstant

Actions Status Build Status Documentation Crates.io LICENSE

A drop-in replacement for std::time::Instant that measures time with high performance and high accuracy powered by TSC.

Usage

[dependencies]
minstant = "0.1"
let start = minstant::Instant::now();

// Code snipppet to measure

let duration: std::time::Duration = start.elapsed();

Motivation

This library is used by a high performance tracing library minitrace-rust. The main purpose is to use TSC on x86 processors to measure time at high speed without losing much accuracy.

Platform Support

Currently, only the Linux on x86 or x86_64 is backed by TSC. On other platforms, minstant falls back to std::time. If TSC is unstable, it will also fall back to std::time.

If speed is privileged over accuracy when fallback occurs, you can use fallback-corase feature to use corase time:

[dependencies]
minstant = { version = "0.1", features = ["fallback-coarse"] }

Benchmark

Benchmark platform is Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz on CentOS 7.

> cargo criterion

Instant::now()/minstant             time:   [10.449 ns 10.514 ns 10.619 ns]
Instant::now()/quanta               time:   [31.467 ns 31.628 ns 31.822 ns]
Instant::now()/std                  time:   [26.831 ns 26.924 ns 27.016 ns]
minstant::Anchor::new()             time:   [46.987 ns 47.243 ns 47.498 ns]
minstant::Instant::as_unix_nanos()  time:   [15.287 ns 15.318 ns 15.350 ns]

Benchmark

Dependencies

~1.1–1.7MB
~38K SLoC