Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_shield::CpuTickTimer Class Reference

Timer that measures CPU time ticks using get_cpu_time(). More...

#include <CpuTickTimer.hpp>

Public Member Functions

 CpuTickTimer (bool is_start_immediately=true) noexcept
 Construct timer and optionally start it immediately.
 
void start () noexcept
 Start measuring CPU time.
 
void restart () noexcept
 Restart timer and reset collected statistics.
 
void stop () noexcept
 Stop measuring CPU time and freeze elapsed ticks.
 
double elapsed () const noexcept
 Get elapsed CPU ticks since the last start.
 
double record_sample () noexcept
 Record sample using elapsed ticks and restart timer.
 
void reset_samples () noexcept
 Reset collected samples without touching running state.
 
std::size_t sample_count () const noexcept
 Get the number of recorded samples.
 
double total_ticks () const noexcept
 Get total recorded CPU ticks across samples.
 
double average_ticks () const noexcept
 Get average CPU ticks per sample.
 
double last_sample_ticks () const noexcept
 Get ticks collected during the last recorded sample.
 

Private Member Functions

void accumulate_ticks (double sample_ticks) noexcept
 

Private Attributes

double m_start_ticks { 0.0 }
 
double m_end_ticks { 0.0 }
 
double m_total_ticks { 0.0 }
 
double m_total_compensation { 0.0 }
 
double m_last_sample_ticks { 0.0 }
 
std::size_t m_sample_count { 0 }
 
bool m_is_running { false }
 

Detailed Description

Timer that measures CPU time ticks using get_cpu_time().

Class is intended for single-threaded use and assumes that get_cpu_time() is monotonic within the current process or thread. For long-running measurements (for example, durations longer than a day) it is recommended to periodically call record_sample() or restart() to limit floating-point precision loss.

Note
All reported durations are expressed in CPU tick units provided by get_cpu_time().

Definition at line 25 of file CpuTickTimer.hpp.

Constructor & Destructor Documentation

◆ CpuTickTimer()

time_shield::CpuTickTimer::CpuTickTimer ( bool is_start_immediately = true)
inlineexplicitnoexcept

Construct timer and optionally start it immediately.

Parameters
is_start_immediatelyIndicates whether the timer should start right away.

Definition at line 29 of file CpuTickTimer.hpp.

Member Function Documentation

◆ accumulate_ticks()

void time_shield::CpuTickTimer::accumulate_ticks ( double sample_ticks)
inlineprivatenoexcept

Definition at line 119 of file CpuTickTimer.hpp.

◆ average_ticks()

double time_shield::CpuTickTimer::average_ticks ( ) const
inlinenodiscardnoexcept

Get average CPU ticks per sample.

Returns
Average CPU tick units or NaN if there are no samples.

Definition at line 105 of file CpuTickTimer.hpp.

◆ elapsed()

double time_shield::CpuTickTimer::elapsed ( ) const
inlinenodiscardnoexcept

Get elapsed CPU ticks since the last start.

Returns
Elapsed CPU tick units produced by get_cpu_time().

Definition at line 58 of file CpuTickTimer.hpp.

◆ last_sample_ticks()

double time_shield::CpuTickTimer::last_sample_ticks ( ) const
inlinenodiscardnoexcept

Get ticks collected during the last recorded sample.

Returns
Ticks from the most recent sample in CPU tick units.

Definition at line 114 of file CpuTickTimer.hpp.

◆ record_sample()

double time_shield::CpuTickTimer::record_sample ( )
inlinenoexcept

Record sample using elapsed ticks and restart timer.

Returns
Collected sample value in CPU tick units or 0.0 when the timer was not previously running.

Definition at line 66 of file CpuTickTimer.hpp.

◆ reset_samples()

void time_shield::CpuTickTimer::reset_samples ( )
inlinenoexcept

Reset collected samples without touching running state.

Definition at line 84 of file CpuTickTimer.hpp.

◆ restart()

void time_shield::CpuTickTimer::restart ( )
inlinenoexcept

Restart timer and reset collected statistics.

Definition at line 43 of file CpuTickTimer.hpp.

◆ sample_count()

std::size_t time_shield::CpuTickTimer::sample_count ( ) const
inlinenodiscardnoexcept

Get the number of recorded samples.

Returns
Count of recorded samples.

Definition at line 93 of file CpuTickTimer.hpp.

◆ start()

void time_shield::CpuTickTimer::start ( )
inlinenoexcept

Start measuring CPU time.

Definition at line 36 of file CpuTickTimer.hpp.

◆ stop()

void time_shield::CpuTickTimer::stop ( )
inlinenoexcept

Stop measuring CPU time and freeze elapsed ticks.

Definition at line 49 of file CpuTickTimer.hpp.

◆ total_ticks()

double time_shield::CpuTickTimer::total_ticks ( ) const
inlinenodiscardnoexcept

Get total recorded CPU ticks across samples.

Returns
Sum of recorded CPU tick units.

Definition at line 99 of file CpuTickTimer.hpp.

Member Data Documentation

◆ m_end_ticks

double time_shield::CpuTickTimer::m_end_ticks { 0.0 }
private

Definition at line 127 of file CpuTickTimer.hpp.

◆ m_is_running

bool time_shield::CpuTickTimer::m_is_running { false }
private

Definition at line 132 of file CpuTickTimer.hpp.

◆ m_last_sample_ticks

double time_shield::CpuTickTimer::m_last_sample_ticks { 0.0 }
private

Definition at line 130 of file CpuTickTimer.hpp.

◆ m_sample_count

std::size_t time_shield::CpuTickTimer::m_sample_count { 0 }
private

Definition at line 131 of file CpuTickTimer.hpp.

◆ m_start_ticks

double time_shield::CpuTickTimer::m_start_ticks { 0.0 }
private

Definition at line 126 of file CpuTickTimer.hpp.

◆ m_total_compensation

double time_shield::CpuTickTimer::m_total_compensation { 0.0 }
private

Definition at line 129 of file CpuTickTimer.hpp.

◆ m_total_ticks

double time_shield::CpuTickTimer::m_total_ticks { 0.0 }
private

Definition at line 128 of file CpuTickTimer.hpp.


The documentation for this class was generated from the following file: