Time Shield Library
C++ library for working with time
Toggle main menu visibility
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_HEADER_TIME_SHIELD_CPUTICKTIMER_HPP_INCLUDED
4
#define TIME_SHIELD_HEADER_TIME_SHIELD_CPUTICKTIMER_HPP_INCLUDED
5
8
9
#include "
time_utils.hpp
"
10
11
#include <cstddef>
12
#include <limits>
13
14
namespace
time_shield
{
15
25
class
CpuTickTimer
{
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 {
37
m_start_ticks
=
get_cpu_time
();
38
m_end_ticks
=
m_start_ticks
;
39
m_is_running
=
true
;
40
}
41
43
void
restart
() noexcept {
44
reset_samples
();
45
start
();
46
}
47
49
void
stop
() noexcept {
50
if
(
m_is_running
) {
51
m_end_ticks
=
get_cpu_time
();
52
m_is_running
=
false
;
53
}
54
}
55
58
TIME_SHIELD_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
();
69
m_last_sample_ticks
= 0.0;
70
return
0.0;
71
}
72
73
const
double
now_ticks =
get_cpu_time
();
74
m_last_sample_ticks
= now_ticks -
m_start_ticks
;
75
m_start_ticks
= now_ticks;
76
77
accumulate_ticks
(
m_last_sample_ticks
);
78
++
m_sample_count
;
79
80
return
m_last_sample_ticks
;
81
}
82
84
void
reset_samples
() noexcept {
85
m_total_ticks
= 0.0;
86
m_total_compensation
= 0.0;
87
m_last_sample_ticks
= 0.0;
88
m_sample_count
= 0;
89
}
90
93
TIME_SHIELD_NODISCARD std::size_t
sample_count
() const noexcept {
94
return
m_sample_count
;
95
}
96
99
TIME_SHIELD_NODISCARD
double
total_ticks
() const noexcept {
100
return
m_total_ticks
;
101
}
102
105
TIME_SHIELD_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
TIME_SHIELD_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_HEADER_TIME_SHIELD_CPUTICKTIMER_HPP_INCLUDED
time_shield::CpuTickTimer::m_start_ticks
double m_start_ticks
Definition
CpuTickTimer.hpp:126
time_shield::CpuTickTimer::total_ticks
TIME_SHIELD_NODISCARD double total_ticks() const noexcept
Get total recorded CPU ticks across samples.
Definition
CpuTickTimer.hpp:99
time_shield::CpuTickTimer::last_sample_ticks
TIME_SHIELD_NODISCARD double last_sample_ticks() const noexcept
Get ticks collected during the last recorded sample.
Definition
CpuTickTimer.hpp:114
time_shield::CpuTickTimer::elapsed
TIME_SHIELD_NODISCARD double elapsed() const noexcept
Get elapsed CPU ticks since the last start.
Definition
CpuTickTimer.hpp:58
time_shield::CpuTickTimer::sample_count
TIME_SHIELD_NODISCARD std::size_t sample_count() const noexcept
Get the number of recorded samples.
Definition
CpuTickTimer.hpp:93
time_shield::CpuTickTimer::accumulate_ticks
void accumulate_ticks(double sample_ticks) noexcept
Definition
CpuTickTimer.hpp:119
time_shield::CpuTickTimer::reset_samples
void reset_samples() noexcept
Reset collected samples without touching running state.
Definition
CpuTickTimer.hpp:84
time_shield::CpuTickTimer::stop
void stop() noexcept
Stop measuring CPU time and freeze elapsed ticks.
Definition
CpuTickTimer.hpp:49
time_shield::CpuTickTimer::m_last_sample_ticks
double m_last_sample_ticks
Definition
CpuTickTimer.hpp:130
time_shield::CpuTickTimer::record_sample
double record_sample() noexcept
Record sample using elapsed ticks and restart timer.
Definition
CpuTickTimer.hpp:66
time_shield::CpuTickTimer::average_ticks
TIME_SHIELD_NODISCARD double average_ticks() const noexcept
Get average CPU ticks per sample.
Definition
CpuTickTimer.hpp:105
time_shield::CpuTickTimer::CpuTickTimer
CpuTickTimer(bool is_start_immediately=true) noexcept
Construct timer and optionally start it immediately.
Definition
CpuTickTimer.hpp:29
time_shield::CpuTickTimer::m_end_ticks
double m_end_ticks
Definition
CpuTickTimer.hpp:127
time_shield::CpuTickTimer::m_is_running
bool m_is_running
Definition
CpuTickTimer.hpp:132
time_shield::CpuTickTimer::m_total_compensation
double m_total_compensation
Definition
CpuTickTimer.hpp:129
time_shield::CpuTickTimer::restart
void restart() noexcept
Restart timer and reset collected statistics.
Definition
CpuTickTimer.hpp:43
time_shield::CpuTickTimer::m_total_ticks
double m_total_ticks
Definition
CpuTickTimer.hpp:128
time_shield::CpuTickTimer::m_sample_count
std::size_t m_sample_count
Definition
CpuTickTimer.hpp:131
time_shield::CpuTickTimer::start
void start() noexcept
Start measuring CPU time.
Definition
CpuTickTimer.hpp:36
time_shield::get_cpu_time
double get_cpu_time() noexcept
Get the CPU time used by the current process.
Definition
time_utils.hpp:284
time_shield
Main namespace for the Time Shield library.
time_utils.hpp
Header file with time-related utility functions.
include
time_shield
CpuTickTimer.hpp
Generated by
1.17.0