Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
CpuTickTimer.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef _TIME_SHIELD_CPU_TICK_TIMER_HPP_INCLUDED
4#define _TIME_SHIELD_CPU_TICK_TIMER_HPP_INCLUDED
5
8
9#include "time_utils.hpp"
10
11#include <cstddef>
12#include <limits>
13
14namespace time_shield {
15
26 public:
29 explicit CpuTickTimer(bool is_start_immediately = true) noexcept {
30 if (is_start_immediately) {
31 start();
32 }
33 }
34
36 void start() noexcept {
39 m_is_running = true;
40 }
41
43 void restart() noexcept {
45 start();
46 }
47
49 void stop() noexcept {
50 if (m_is_running) {
52 m_is_running = false;
53 }
54 }
55
58 [[nodiscard]] double elapsed() const noexcept {
59 const double final_ticks = m_is_running ? get_cpu_time() : m_end_ticks;
60 return final_ticks - m_start_ticks;
61 }
62
66 double record_sample() noexcept {
67 if (!m_is_running) {
68 start();
70 return 0.0;
71 }
72
73 const double now_ticks = get_cpu_time();
75 m_start_ticks = now_ticks;
76
79
81 }
82
84 void reset_samples() noexcept {
85 m_total_ticks = 0.0;
89 }
90
93 [[nodiscard]] std::size_t sample_count() const noexcept {
94 return m_sample_count;
95 }
96
99 [[nodiscard]] double total_ticks() const noexcept {
100 return m_total_ticks;
101 }
102
105 [[nodiscard]] double average_ticks() const noexcept {
106 if (m_sample_count == 0U) {
107 return std::numeric_limits<double>::quiet_NaN();
108 }
109 return m_total_ticks / static_cast<double>(m_sample_count);
110 }
111
114 [[nodiscard]] double last_sample_ticks() const noexcept {
115 return m_last_sample_ticks;
116 }
117
118 private:
119 void accumulate_ticks(double sample_ticks) noexcept {
120 const double compensated = sample_ticks - m_total_compensation;
121 const double updated_total = m_total_ticks + compensated;
122 m_total_compensation = (updated_total - m_total_ticks) - compensated;
123 m_total_ticks = updated_total;
124 }
125
126 double m_start_ticks { 0.0 };
127 double m_end_ticks { 0.0 };
128 double m_total_ticks { 0.0 };
129 double m_total_compensation { 0.0 };
130 double m_last_sample_ticks { 0.0 };
131 std::size_t m_sample_count { 0 };
132 bool m_is_running { false };
133 };
134
135} // namespace time_shield
136
137#endif // _TIME_SHIELD_CPU_TICK_TIMER_HPP_INCLUDED
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.
void accumulate_ticks(double sample_ticks) noexcept
double total_ticks() const noexcept
Get total recorded CPU ticks across samples.
void reset_samples() noexcept
Reset collected samples without touching running state.
void stop() noexcept
Stop measuring CPU time and freeze elapsed ticks.
double record_sample() noexcept
Record sample using elapsed ticks and restart timer.
CpuTickTimer(bool is_start_immediately=true) noexcept
Construct timer and optionally start it immediately.
double elapsed() const noexcept
Get elapsed CPU ticks since the last start.
void restart() noexcept
Restart timer and reset collected statistics.
std::size_t sample_count() const noexcept
Get the number of recorded samples.
void start() noexcept
Start measuring CPU time.
double get_cpu_time() noexcept
Get the CPU time used by the current process.
Main namespace for the Time Shield library.
Header file with time-related utility functions.