Atomic, high-resolution elapsed-time timer.
More...
#include <elapsed_timer.h>
Atomic, high-resolution elapsed-time timer.
Measures elapsed time since start() using either a monotonic wall-clock (kCpuTimestamp) or the process CPU-active time (kCpuActiveTime). The class is final and not copyable (copy/move constructors are provided but only copy the snapshot value, not ownership semantics).
◆ Accuracy
Selects the precision (resolution) of the timer values returned.
| Enumerator |
|---|
| kMilli | Millisecond precision.
|
| kMicro | Microsecond precision.
|
| kNano | Nanosecond precision.
|
◆ Method
Selects the underlying clock source for time measurement.
| Enumerator |
|---|
| kCpuTimestamp | Monotonic wall-clock (CLOCK_MONOTONIC_RAW on Linux).
|
| kCpuActiveTime | Process CPU time (user + kernel, via getrusage).
|
◆ ElapsedTimer() [1/6]
| vlink::ElapsedTimer::ElapsedTimer |
( |
| ) |
|
|
noexcept |
Constructs an ElapsedTimer with default method (kCpuTimestamp) and default accuracy (kMilli).
The timer is not started after construction.
◆ ElapsedTimer() [2/6]
| vlink::ElapsedTimer::ElapsedTimer |
( |
Method | method | ) |
|
|
explicitnoexcept |
Constructs an ElapsedTimer with the specified clock source.
- Parameters
-
| method | The clock source to use (kCpuTimestamp or kCpuActiveTime). |
◆ ElapsedTimer() [3/6]
| vlink::ElapsedTimer::ElapsedTimer |
( |
Accuracy | accuracy | ) |
|
|
explicitnoexcept |
Constructs an ElapsedTimer with the specified precision.
- Parameters
-
| accuracy | The precision of time values (kMilli, kMicro, or kNano). |
◆ ElapsedTimer() [4/6]
| vlink::ElapsedTimer::ElapsedTimer |
( |
Method | method, |
|
|
Accuracy | accuracy ) |
|
explicitnoexcept |
Constructs an ElapsedTimer with the specified clock source and precision.
- Parameters
-
| method | The clock source to use. |
| accuracy | The precision of time values. |
◆ ElapsedTimer() [5/6]
| vlink::ElapsedTimer::ElapsedTimer |
( |
const ElapsedTimer & | target | ) |
|
|
noexcept |
Copy constructor. Copies the current start-time snapshot, method, and accuracy.
- Parameters
-
◆ ElapsedTimer() [6/6]
| vlink::ElapsedTimer::ElapsedTimer |
( |
ElapsedTimer && | target | ) |
|
|
noexcept |
Move constructor. Behaves identically to the copy constructor.
- Parameters
-
◆ ~ElapsedTimer()
| vlink::ElapsedTimer::~ElapsedTimer |
( |
| ) |
|
|
noexcept |
◆ get()
| int64_t vlink::ElapsedTimer::get |
( |
| ) |
const |
|
nodiscardnoexcept |
Returns the elapsed time since start() was called.
The value is computed as (current_time - start_time_) in the configured precision units. Returns -1 if the timer has not been started (or has been stopped).
- Returns
- Elapsed time in the configured units (>= 0) when active.
-1 when the timer is not active.
◆ get_accuracy()
| Accuracy vlink::ElapsedTimer::get_accuracy |
( |
| ) |
const |
|
nodiscardnoexcept |
Returns the precision configured for this timer.
- Returns
- The
Accuracy value set at construction.
◆ get_cpu_active_time()
| uint64_t vlink::ElapsedTimer::get_cpu_active_time |
( |
Accuracy | accuracy = kMilli | ) |
|
|
staticnodiscardnoexcept |
Returns the accumulated CPU time (user + kernel) consumed by the current process.
Uses getrusage(RUSAGE_SELF) on POSIX or GetProcessTimes on Windows. The returned value is the total CPU time used so far in the process lifetime, not a delta. Use ElapsedTimer with kCpuActiveTime to measure deltas.
- Parameters
-
| accuracy | Desired precision (default: kMilli). |
- Returns
- Cumulative process CPU time in the requested unit.
◆ get_cpu_timestamp()
| uint64_t vlink::ElapsedTimer::get_cpu_timestamp |
( |
Accuracy | accuracy = kMilli, |
|
|
bool | high_resolution = true ) |
|
staticnodiscardnoexcept |
Returns the current monotonic CPU timestamp.
On Linux uses CLOCK_MONOTONIC_RAW (unaffected by NTP) when high_resolution is true; otherwise uses std::chrono::steady_clock. On Windows always uses std::chrono::steady_clock.
- Parameters
-
| accuracy | Desired precision (default: kMilli). |
| high_resolution | Whether to use clock_gettime (Linux only, default: true). |
- Returns
- Current monotonic timestamp in the requested unit.
◆ get_method()
| Method vlink::ElapsedTimer::get_method |
( |
| ) |
const |
|
nodiscardnoexcept |
Returns the clock source configured for this timer.
- Returns
- The
Method value set at construction.
◆ get_sys_timestamp()
| uint64_t vlink::ElapsedTimer::get_sys_timestamp |
( |
Accuracy | accuracy = kMilli, |
|
|
bool | high_resolution = true ) |
|
staticnodiscardnoexcept |
Returns the current wall-clock (system) timestamp.
On Linux uses CLOCK_REALTIME via clock_gettime for maximum precision when high_resolution is true; otherwise uses std::chrono::system_clock. On Windows always uses std::chrono::system_clock.
- Parameters
-
| accuracy | Desired precision (default: kMilli). |
| high_resolution | Whether to use clock_gettime (Linux only, default: true). |
- Returns
- Current system timestamp in the requested unit.
◆ is_active()
| bool vlink::ElapsedTimer::is_active |
( |
| ) |
const |
|
nodiscardnoexcept |
Returns true when the timer has been started and not yet stopped.
- Returns
true if active (start_time_ >= 0), false otherwise.
◆ operator=() [1/2]
Copy-assignment operator.
- Parameters
-
- Returns
- A reference to
*this.
◆ operator=() [2/2]
Move-assignment operator.
- Parameters
-
| target | The source timer (moved from). |
- Returns
- A reference to
*this.
◆ restart()
| int64_t vlink::ElapsedTimer::restart |
( |
| ) |
|
|
noexcept |
Atomically resets the start time to now and returns the elapsed time since the previous start() / restart() call.
This is equivalent to get() followed by start(), but performed atomically using exchange. If the timer was not active, returns a negative value (the raw start_time_ value, which is -1 when stopped).
- Returns
- Elapsed time in the configured units since the last start/restart, or
- A negative value if the timer was not started before this call.
◆ start()
| void vlink::ElapsedTimer::start |
( |
| ) |
|
|
noexcept |
Starts the timer if it is not already active.
Records the current time as the start reference using a CAS on the internal atomic. If the timer is already active this call is a no-op. Call stop() first to reset and then start() again.
◆ stop()
| void vlink::ElapsedTimer::stop |
( |
| ) |
|
|
noexcept |
Stops the timer, setting the internal start time to -1.
After stop(), is_active() returns false and get() returns -1.
The documentation for this class was generated from the following file: