pub struct Clock { /* private fields */ }Expand description
A clock that can efficiently provide the current time.
This is suitable for querying rapidly with low overhead, in circumstances where absolute precision is not necessary. The timestamps produced by this clock are monotonically increasing but may lag behind wall-clock time by a small number of milliseconds and may not follow explicit wall clock adjustments applied by the operating system (e.g. as part of clock synchronization).
This typically makes these timestamps useful for rapid polling scenarios, such as metrics and logging, where efficiency matters greatly because you may be capturing 100 000 timestamps per second, whereas the precise microsecond or even millisecond is not important.
§Examples
Creating a clock and capturing timestamps:
use fast_time::Clock;
let mut clock = Clock::new();
let instant1 = clock.now();
let instant2 = clock.now();
// Timestamps are monotonically increasing
assert!(instant2.saturating_duration_since(instant1).as_nanos() >= 0);Measuring elapsed time:
use std::time::Duration;
use fast_time::Clock;
let mut clock = Clock::new();
let start = clock.now();
// Simulate some work
std::thread::sleep(Duration::from_millis(5));
let elapsed = start.elapsed(&mut clock);
// Note: fast_time prioritizes efficiency over precision, so we use loose tolerance
assert!(elapsed <= Duration::from_millis(50)); // Very generous upper boundHigh-frequency timestamp collection:
use fast_time::Clock;
let mut clock = Clock::new();
let mut durations = Vec::new();
let start = clock.now();
for _ in 0..1000 {
let timestamp = clock.now();
durations.push(timestamp.saturating_duration_since(start));
}
// All durations should be monotonically increasing
for window in durations.windows(2) {
assert!(window[1] >= window[0]);
}Implementations§
Source§impl Clock
impl Clock
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new clock instance.
The clock will use the platform’s most efficient time source for rapid timestamp capture.
§Examples
use fast_time::Clock;
let mut clock = Clock::new();
let timestamp = clock.now();Sourcepub fn now(&mut self) -> Instant
pub fn now(&mut self) -> Instant
Returns the current timestamp.
This method is optimized for rapid, repeated calls. The returned Instant
represents a point in time that can be used to measure elapsed duration.
§Examples
Basic timestamp capture:
use fast_time::Clock;
let mut clock = Clock::new();
let now = clock.now();
println!("Current time: {:?}", now);Rapid timestamp collection:
use fast_time::Clock;
let mut clock = Clock::new();
let timestamps: Vec<_> = (0..100).map(|_| clock.now()).collect();
// All timestamps should be valid
assert_eq!(timestamps.len(), 100);