| Crates.io | high_precision_clock |
| lib.rs | high_precision_clock |
| version | 0.1.9 |
| created_at | 2024-10-26 09:56:26.344583+00 |
| updated_at | 2024-10-27 04:50:27.266706+00 |
| description | A high-precision clock for cloud servers. |
| homepage | https://github.com/alphapku/high_precision_clock |
| repository | https://github.com/alphapku/high_precision_clock |
| max_upload_size | |
| id | 1423697 |
| size | 12,164 |
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.
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
}