Skip to main content

Crate fast_clock

Crate fast_clock 

Source
Expand description

Low-overhead timing without hiding the details from you.

§Core abstractions

§Standard library clocks

std_clocks::InstantClock and std_clocks::SystemClock wrap std::time::Instant and std::time::SystemTime. They use InherentlyCalibrated, as their duration type is directly convertible to/from nanoseconds.

§Hardware clocks

tsc::Tsc reads the x86_64 timestamp counter. Its ticks are not nanoseconds, so calibration via wrapping_u64::U64Calibration is required. Calibration also produces a ClockSynchronization that can convert TSC instants to std::time::Instant and vice versa.


let tsc = Tsc::try_new_assume_stable().unwrap();
let (calibration, sync) = U64Calibration::new_with_std_instant(
    &tsc,
    std::time::Duration::from_millis(100),
);
let clock = CalibratedClock { clock: tsc, calibration };

let t0 = clock.clock.now();
// ... timed section ...
let t1 = clock.clock.now();

let duration = WrappingU64Time::instant_sub(t1, t0);
let duration_ns: u64 = clock.calibration.convert_to_ns(duration);

// Convert a TSC instant to std::time::Instant using the synchronization point.
let std_instant = sync.to_a(t0, &InherentlyCalibrated, &clock.calibration);

§Features

FeatureDefaultDescription
stdyesEnables std_clocks and std-dependent methods.
tscyesEnables tsc (x86_64 only).

Contributions adding more clocks are welcome.

Modules§

std_clocks
tsc
wrapping_u64

Structs§

CalibratedClock
A Clock bundled with its DurationCalibration.
ClockSynchronization
A pair of instants from two clocks that correspond to roughly the same point in time.
InherentlyCalibrated
Calibration type for clocks whose native duration is already in nanoseconds.

Traits§

Clock
A source of time readings.
DurationCalibration
Converts a clock’s native duration type to and from nanoseconds.
Time
Arithmetic types and operations for a clock domain.