structstd.time.Timer[src]

A monotonic, high performance timer.

Timer.start() is used to initialize the timer and gives the caller an opportunity to check for the existence of a supported clock. Once a supported clock is discovered, it is assumed that it will be available for the duration of the Timer's use.

Monotonicity is ensured by saturating on the most previous sample. This means that while timings reported are monotonic, they're not guaranteed to tick at a steady rate as this is up to the underlying system.

Fields

started: Instant
previous: Instant

Error Sets

Error SetError[src]

Errors

anyerror means the error set is known only at runtime.

TimerUnsupported

Source Code

Source code
pub const Error = error{TimerUnsupported}

Functions

Functionstart[src]

pub fn start() Error!Timer

Initialize the timer by querying for a supported clock. Returns error.TimerUnsupported when such a clock is unavailable. This should only fail in hostile environments such as linux seccomp misuse.

Source Code

Source code
pub fn start() Error!Timer {
    const current = Instant.now() catch return error.TimerUnsupported;
    return Timer{ .started = current, .previous = current };
}

Functionread[src]

pub fn read(self: *Timer) u64

Reads the timer value since start or the last reset in nanoseconds.

Parameters

self: *Timer

Source Code

Source code
pub fn read(self: *Timer) u64 {
    const current = self.sample();
    return current.since(self.started);
}

Functionreset[src]

pub fn reset(self: *Timer) void

Resets the timer value to 0/now.

Parameters

self: *Timer

Source Code

Source code
pub fn reset(self: *Timer) void {
    const current = self.sample();
    self.started = current;
}

Functionlap[src]

pub fn lap(self: *Timer) u64

Returns the current value of the timer in nanoseconds, then resets it.

Parameters

self: *Timer

Source Code

Source code
pub fn lap(self: *Timer) u64 {
    const current = self.sample();
    defer self.started = current;
    return current.since(self.started);
}

Source Code

Source code
pub const Timer = struct {
    started: Instant,
    previous: Instant,

    pub const Error = error{TimerUnsupported};

    /// Initialize the timer by querying for a supported clock.
    /// Returns `error.TimerUnsupported` when such a clock is unavailable.
    /// This should only fail in hostile environments such as linux seccomp misuse.
    pub fn start() Error!Timer {
        const current = Instant.now() catch return error.TimerUnsupported;
        return Timer{ .started = current, .previous = current };
    }

    /// Reads the timer value since start or the last reset in nanoseconds.
    pub fn read(self: *Timer) u64 {
        const current = self.sample();
        return current.since(self.started);
    }

    /// Resets the timer value to 0/now.
    pub fn reset(self: *Timer) void {
        const current = self.sample();
        self.started = current;
    }

    /// Returns the current value of the timer in nanoseconds, then resets it.
    pub fn lap(self: *Timer) u64 {
        const current = self.sample();
        defer self.started = current;
        return current.since(self.started);
    }

    /// Returns an Instant sampled at the callsite that is
    /// guaranteed to be monotonic with respect to the timer's starting point.
    fn sample(self: *Timer) Instant {
        const current = Instant.now() catch unreachable;
        if (current.order(self.previous) == .gt) {
            self.previous = current;
        }
        return self.previous;
    }
}