pub struct NanoMonotonicClock { /* private fields */ }Expand description
A clock implementation that provides nanosecond-precision monotonic time.
This clock combines the monotonic guarantees of
MonotonicClock with the nanosecond precision
of NanoClock. It uses std::time::Instant as its time source and
stores the base time with nanosecond precision.
§Use Cases
- High-precision performance testing
- Microbenchmarking
- Scenarios requiring nanosecond-level time measurements
§Thread Safety
This type is completely thread-safe as all fields are immutable after creation.
§Examples
use qubit_clock::{NanoClock, NanoMonotonicClock};
let clock = NanoMonotonicClock::new();
let start = clock.nanos();
// Perform some operation
for _ in 0..1000 {
// Some work
}
let elapsed = clock.nanos() - start;
println!("Elapsed: {} ns", elapsed);§Author
Haixing Hu
Implementations§
Source§impl NanoMonotonicClock
impl NanoMonotonicClock
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new NanoMonotonicClock.
The clock records the current instant and system time (with nanosecond precision) as its base point. All subsequent time queries will be calculated relative to this base point.
§Returns
A new NanoMonotonicClock instance.
§Examples
use qubit_clock::NanoMonotonicClock;
let clock = NanoMonotonicClock::new();Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the elapsed monotonic duration since this clock was created.
This value is based purely on Instant and is not affected by system
time adjustments.
§Returns
The elapsed monotonic duration.
§Examples
use qubit_clock::NanoMonotonicClock;
use std::thread;
use std::time::Duration;
let clock = NanoMonotonicClock::new();
thread::sleep(Duration::from_millis(10));
assert!(clock.elapsed() >= Duration::from_millis(10));Sourcepub fn monotonic_nanos(&self) -> i128
pub fn monotonic_nanos(&self) -> i128
Returns the elapsed monotonic time in nanoseconds since creation.
Unlike NanoClock::nanos, this value does
not include a wall-clock epoch anchor and is intended for interval
measurement.
§Returns
The elapsed monotonic nanoseconds, saturated at i128::MAX.
§Examples
use qubit_clock::NanoMonotonicClock;
use std::thread;
use std::time::Duration;
let clock = NanoMonotonicClock::new();
thread::sleep(Duration::from_millis(10));
assert!(clock.monotonic_nanos() >= 10_000_000);Trait Implementations§
Source§impl Clock for NanoMonotonicClock
impl Clock for NanoMonotonicClock
Source§impl Clone for NanoMonotonicClock
impl Clone for NanoMonotonicClock
Source§fn clone(&self) -> NanoMonotonicClock
fn clone(&self) -> NanoMonotonicClock
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more