NanoTimeMeter

Struct NanoTimeMeter 

Source
pub struct NanoTimeMeter<C: NanoClock> { /* private fields */ }
Expand description

A time meter for measuring elapsed time with nanosecond precision.

This is the high-precision version of TimeMeter, specifically designed for scenarios requiring nanosecond-level precision, such as microbenchmarking and high-frequency operation performance analysis.

§Differences from TimeMeter

  • Precision: NanoTimeMeter provides nanosecond precision, TimeMeter provides millisecond precision
  • Performance: NanoTimeMeter has slightly higher computational overhead, but offers higher precision
  • Use Cases: NanoTimeMeter is suitable for microbenchmarking, TimeMeter is suitable for general business monitoring

§Thread Safety

This type is not thread-safe. If you need to use it in a multi-threaded environment, you should create separate instances for each thread or use external synchronization mechanisms.

§Examples

use prism3_clock::meter::NanoTimeMeter;

// Basic usage
let mut meter = NanoTimeMeter::new();
meter.start();
// Perform high-frequency operations
meter.stop();
println!("Elapsed: {} nanos", meter.nanos());
println!("Elapsed: {}", meter.readable_duration());

§Author

Haixing Hu

Implementations§

Source§

impl<C: NanoClock> NanoTimeMeter<C>

Source

pub fn with_clock(clock: C) -> Self

Creates a new nano time meter with the specified clock.

§Arguments
  • clock - The clock to use for time measurement
§Returns

A new NanoTimeMeter instance

§Examples
use prism3_clock::{NanoMonotonicClock, meter::NanoTimeMeter};

let clock = NanoMonotonicClock::new();
let meter = NanoTimeMeter::with_clock(clock);
Source

pub fn with_clock_started(clock: C) -> Self

Creates a new nano time meter with the specified clock and starts it immediately.

§Arguments
  • clock - The clock to use for time measurement
§Returns

A new NanoTimeMeter instance that has already been started

§Examples
use prism3_clock::{NanoMonotonicClock, meter::NanoTimeMeter};

let clock = NanoMonotonicClock::new();
let meter = NanoTimeMeter::with_clock_started(clock);
Source

pub fn start(&mut self)

Starts this meter.

Records the current time as the start timestamp. If the meter has already been started, this operation will restart timing.

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::new();
meter.start();
Source

pub fn stop(&mut self)

Stops this meter.

Records the current time as the end timestamp. After calling this method, duration() will return a fixed time interval until start() or reset() is called again.

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::start_now();
// Do some work
meter.stop();
Source

pub fn reset(&mut self)

Resets this meter.

Clears the start and end timestamps, restoring the meter to its initial state. After reset, you need to call start() again to begin a new time measurement.

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::start_now();
// Do some work
meter.stop();
meter.reset();
Source

pub fn restart(&mut self)

Resets and immediately starts this meter.

This is equivalent to calling reset() followed by start().

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::start_now();
// Do some work
meter.restart();
// Do more work
Source

pub fn nanos(&self) -> i128

Returns the elapsed duration in nanoseconds.

If the meter has been stopped (by calling stop()), returns the time interval from start to stop. If the meter has not been stopped, returns the time interval from start to the current moment.

If the meter has not been started (by calling start()), returns 0.

To avoid overflow issues, this method uses safe arithmetic operations.

§Returns

The elapsed duration in nanoseconds

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::start_now();
// Do some work
assert!(meter.nanos() > 0);
Source

pub fn micros(&self) -> i128

Returns the elapsed duration in microseconds.

This method is based on the result of nanos(), converting nanoseconds to microseconds.

§Returns

The elapsed duration in microseconds

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::start_now();
// Do some work
assert!(meter.micros() >= 0);
Source

pub fn millis(&self) -> i64

Returns the elapsed duration in milliseconds.

This method is based on the result of nanos(), converting nanoseconds to milliseconds.

If the meter has not been started (by calling start()), returns 0.

§Returns

The elapsed duration in milliseconds

§Examples
use prism3_clock::meter::NanoTimeMeter;
use std::thread;
use std::time::Duration;

let mut meter = NanoTimeMeter::start_now();
thread::sleep(Duration::from_millis(100));
assert!(meter.millis() >= 100);
Source

pub fn seconds(&self) -> i64

Returns the elapsed duration in seconds.

This method is based on the result of nanos(), converting nanoseconds to seconds (rounded down).

§Returns

The elapsed duration in seconds

§Examples
use prism3_clock::meter::NanoTimeMeter;
use std::thread;
use std::time::Duration;

let mut meter = NanoTimeMeter::start_now();
thread::sleep(Duration::from_secs(1));
assert!(meter.seconds() >= 1);
Source

pub fn minutes(&self) -> i64

Returns the elapsed duration in minutes.

This method is based on the result of nanos(), converting nanoseconds to minutes (rounded down).

§Returns

The elapsed duration in minutes

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::new();
meter.start();
// Simulate some time
meter.stop();
Source

pub fn duration(&self) -> Duration

Returns the elapsed duration as a Duration object.

If the meter has been stopped (by calling stop()), returns the time interval from start to stop. If the meter has not been stopped, returns the time interval from start to the current moment.

If the meter has not been started (by calling start()), returns a zero duration.

The returned Duration object has nanosecond precision.

§Returns

The elapsed duration as a Duration object (nanosecond precision)

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::start_now();
let duration = meter.duration();
Source

pub fn readable_duration(&self) -> String

Returns a human-readable string representation of the elapsed duration.

Formats the duration into an easy-to-read string, such as “1h 23m 45s” or “2.5s”.

§Returns

A human-readable string representation of the duration

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::start_now();
// Do some work
meter.stop();
println!("Elapsed: {}", meter.readable_duration());
Source

pub fn speed_per_second(&self, count: usize) -> Option<f64>

Calculates the per-second speed for a given count.

Computes the average count processed per second during the elapsed time. Useful for performance monitoring and speed analysis.

§Arguments
  • count - The count value to calculate speed for
§Returns

The per-second speed, or None if the elapsed time is zero

§Examples
use prism3_clock::meter::NanoTimeMeter;
use std::thread;
use std::time::Duration;

let mut meter = NanoTimeMeter::start_now();
thread::sleep(Duration::from_secs(1));
meter.stop();
if let Some(speed) = meter.speed_per_second(1000) {
    println!("Speed: {:.2} items/s", speed);
}
Source

pub fn speed_per_minute(&self, count: usize) -> Option<f64>

Calculates the per-minute speed for a given count.

Computes the average count processed per minute during the elapsed time. Useful for performance monitoring and speed analysis.

§Arguments
  • count - The count value to calculate speed for
§Returns

The per-minute speed, or None if the elapsed time is zero

§Examples
use prism3_clock::meter::NanoTimeMeter;
use std::thread;
use std::time::Duration;

let mut meter = NanoTimeMeter::start_now();
thread::sleep(Duration::from_secs(1));
meter.stop();
if let Some(speed) = meter.speed_per_minute(1000) {
    println!("Speed: {:.2} items/m", speed);
}
Source

pub fn formatted_speed_per_second(&self, count: usize) -> String

Returns a formatted string of the per-second speed for a given count.

§Arguments
  • count - The count value to calculate speed for
§Returns

A string in the format “{speed}/s”, or “N/A” if the elapsed time is zero

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::start_now();
// Do some work
meter.stop();
println!("Speed: {}", meter.formatted_speed_per_second(1000));
Source

pub fn formatted_speed_per_minute(&self, count: usize) -> String

Returns a formatted string of the per-minute speed for a given count.

§Arguments
  • count - The count value to calculate speed for
§Returns

A string in the format “{speed}/m”, or “N/A” if the elapsed time is zero

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::start_now();
// Do some work
meter.stop();
println!("Speed: {}", meter.formatted_speed_per_minute(1000));
Source

pub fn is_running(&self) -> bool

Checks if the meter is currently running.

§Returns

true if the meter has been started but not stopped, false otherwise

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::new();
assert!(!meter.is_running());
meter.start();
assert!(meter.is_running());
meter.stop();
assert!(!meter.is_running());
Source

pub fn is_stopped(&self) -> bool

Checks if the meter has been stopped.

§Returns

true if the meter has been stopped, false otherwise

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::start_now();
assert!(!meter.is_stopped());
meter.stop();
assert!(meter.is_stopped());
Source

pub fn clock(&self) -> &C

Returns a reference to the clock used by this meter.

§Returns

A reference to the clock

§Examples
use prism3_clock::meter::NanoTimeMeter;

let meter = NanoTimeMeter::new();
let clock = meter.clock();
Source

pub fn clock_mut(&mut self) -> &mut C

Returns a mutable reference to the clock used by this meter.

§Returns

A mutable reference to the clock

§Examples
use prism3_clock::meter::NanoTimeMeter;

let mut meter = NanoTimeMeter::new();
let clock = meter.clock_mut();
Source§

impl NanoTimeMeter<NanoMonotonicClock>

Source

pub fn new() -> Self

Creates a new nano time meter using the default NanoMonotonicClock.

The default clock uses NanoMonotonicClock, which is based on Instant and is not affected by system time adjustments, making it more suitable for high-precision time measurement.

§Returns

A new NanoTimeMeter instance

§Examples
use prism3_clock::meter::NanoTimeMeter;

let meter = NanoTimeMeter::new();
Source

pub fn start_now() -> Self

Creates a new nano time meter using the default NanoMonotonicClock and starts it immediately.

§Returns

A new NanoTimeMeter instance that has already been started

§Examples
use prism3_clock::meter::NanoTimeMeter;

let meter = NanoTimeMeter::start_now();

Trait Implementations§

Source§

impl Default for NanoTimeMeter<NanoMonotonicClock>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<C> Freeze for NanoTimeMeter<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for NanoTimeMeter<C>
where C: RefUnwindSafe,

§

impl<C> Send for NanoTimeMeter<C>

§

impl<C> Sync for NanoTimeMeter<C>

§

impl<C> Unpin for NanoTimeMeter<C>
where C: Unpin,

§

impl<C> UnwindSafe for NanoTimeMeter<C>
where C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.