9 releases
new 0.1.9 | Oct 27, 2024 |
---|---|
0.1.8 | Oct 27, 2024 |
#81 in Finance
489 downloads per month
12KB
160 lines
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
}
lib.rs
:
SimpleHighPrecisionClock
SimpleHighPrecisionClock
provides a high-precision clock that leverages 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
use high_precision_clock::SimpleHighPrecisionClock;
let mut clock = SimpleHighPrecisionClock::new(100 * 1000 * 1000);
let time_ns = clock.now();
println!("Elapsed time in nanoseconds: {}", time_ns);
loop {
clock.calibrate();
// your task
}
This library is particularly useful for applications that require precise time tracking in environments where traditional time sources may lack stability or granularity.
Dependencies
~1MB
~18K SLoC