high_precision_clock

Crates.iohigh_precision_clock
lib.rshigh_precision_clock
version0.1.9
created_at2024-10-26 09:56:26.344583+00
updated_at2024-10-27 04:50:27.266706+00
descriptionA high-precision clock for cloud servers.
homepagehttps://github.com/alphapku/high_precision_clock
repositoryhttps://github.com/alphapku/high_precision_clock
max_upload_size
id1423697
size12,164
(alphapku)

documentation

https://docs.rs/high_precision_clock

README

SimpleHighPrecisionClock

SimpleHighPrecisionClock is a high-precision time source that uses the CPU's Time Stamp Counter (TSC) to measure time elapsed since instantiation in nanoseconds.

The idea is from tscns a very impressive and lightweight clock in C.

This clock is calibrated during initialization to convert TSC ticks to nanoseconds independently of CPU frequency, ensuring high precision and consistent measurements.

The calibrate function should be called from time to time (1 second, for example) to adjust the base tsc and time to keep the precision.

Example

let mut clock = SimpleHighPrecisionClock::new(500*1000*1000);
let time_ns = clock.now();
println!("Elapsed time in nanoseconds: {}", time_ns);
loop {
    clock.calibrate();
    // your task
}
Commit count: 7

cargo fmt