CbmRoot
Loading...
Searching...
No Matches
CaTimer.h
Go to the documentation of this file.
1/* Copyright (C) 2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#pragma once
11
12#include <boost/serialization/serialization.hpp>
13
14#include <chrono>
15#include <cstdint>
16#include <iostream>
17#include <limits>
18#include <string>
19
20namespace cbm::algo::ca
21{
25 class Timer {
26 public:
27 using Clock = std::chrono::high_resolution_clock;
28 using Duration = std::chrono::nanoseconds;
29 using DurationCount = Duration::rep;
30 using TimePoint = std::chrono::time_point<Clock, Duration>;
31
33 Timer() = default;
34
36 ~Timer() = default;
37
39 Timer(const Timer&) = default;
40
42 Timer(Timer&&) = default;
43
45 Timer& operator=(const Timer&) = default;
46
48 Timer& operator=(Timer&&) = default;
49
56 void AddTimer(const Timer& other, bool parallel);
57
59 double GetAverage() const { return static_cast<double>(fTotal) / fNofCalls * 1.e-9; }
60
62 double GetMax() const { return static_cast<double>(fMax) * 1.e-9; }
63
65 double GetMin() const { return static_cast<double>(fMin) * 1.e-9; }
66
68 int GetNofCalls() const { return fNofCalls; }
69
71 int GetMaxCallIndex() const { return fMaxCallIndex; }
72
74 int GetMinCallIndex() const { return fMinCallIndex; }
75
77 double GetTotal() const { return static_cast<double>(fTotal) * 1.e-9; }
78
80 double GetTotalMs() const { return GetTotal() * 1.e3; }
81
83 void Reset();
84
86 void Start() { fStart = Clock::now(); }
87
89 void Stop();
90
91 private:
93 template<typename Archive>
94 void serialize(Archive& ar, const unsigned int /*version*/)
95 {
96 ar& fMin;
97 ar& fMax;
98 ar& fTotal;
99 ar& fNofCalls;
100 ar& fMinCallIndex;
101 ar& fMaxCallIndex;
102 }
103
105 DurationCount fMin = std::numeric_limits<DurationCount>::max();
106 DurationCount fMax = std::numeric_limits<DurationCount>::min();
108 int fNofCalls = 0;
109 int fMinCallIndex = -1;
110 int fMaxCallIndex = -1;
111 }; // class Timer
112
113
114 // ************************************
115 // ** Inline function definition **
116 // ************************************
117
118 // -------------------------------------------------------------------------------------------------------------------
119 //
120 inline void Timer::AddTimer(const Timer& other, bool parallel)
121 {
122 if (other.fMin < fMin) {
123 fMin = other.fMin;
125 }
126 if (other.fMax > fMax) {
127 fMax = other.fMax;
129 }
130 if (parallel) {
131 if (fTotal < other.fTotal) {
132 fTotal = other.fTotal;
133 }
134 }
135 else {
136 fTotal += other.fTotal;
137 }
138 fNofCalls += other.fNofCalls;
139 }
140
141 // -------------------------------------------------------------------------------------------------------------------
142 //
143 inline void Timer::Reset()
144 {
145 fStart = TimePoint();
146 fMin = std::numeric_limits<DurationCount>::max();
147 fMax = std::numeric_limits<DurationCount>::min();
148 fMinCallIndex = -1;
149 fMaxCallIndex = -1;
151 fNofCalls = 0;
152 }
153
154 // -------------------------------------------------------------------------------------------------------------------
155 //
156 inline void Timer::Stop()
157 {
158 auto stop = Clock::now();
159 auto time = std::chrono::duration_cast<Duration>(stop - fStart).count();
160 if (fMin > time) {
161 fMin = time;
163 }
164 if (fMax < time) {
165 fMax = time;
167 }
168 fTotal += time;
169 ++fNofCalls;
170 }
171} // namespace cbm::algo::ca
A timer class for the monitor.
Definition CaTimer.h:25
void serialize(Archive &ar, const unsigned int)
Definition CaTimer.h:94
Timer & operator=(Timer &&)=default
Move assignment operator.
Timer(const Timer &)=default
Copy constructor.
Timer(Timer &&)=default
Move constructor.
DurationCount fTotal
Total measured time period [ns].
Definition CaTimer.h:107
std::chrono::high_resolution_clock Clock
Definition CaTimer.h:27
void Stop()
Stops the timer.
Definition CaTimer.h:156
int fMinCallIndex
Index of the shortest call [ns].
Definition CaTimer.h:109
int GetMaxCallIndex() const
Gets index of the longest call.
Definition CaTimer.h:71
double GetTotalMs() const
Gets total time [ms].
Definition CaTimer.h:80
void AddTimer(const Timer &other, bool parallel)
Adds another timer.
Definition CaTimer.h:120
Timer()=default
Default constructor.
void Start()
Starts the timer.
Definition CaTimer.h:86
Timer & operator=(const Timer &)=default
Copy assignment operator.
double GetTotal() const
Gets total time [s].
Definition CaTimer.h:77
DurationCount fMax
Maximal time period.
Definition CaTimer.h:106
int fNofCalls
Number of timer calls [ns].
Definition CaTimer.h:108
double GetMin() const
Gets time of the shortest call [s].
Definition CaTimer.h:65
std::chrono::nanoseconds Duration
Definition CaTimer.h:28
int fMaxCallIndex
Index of the longest call [ns].
Definition CaTimer.h:110
int GetMinCallIndex() const
Gets index of the longest call.
Definition CaTimer.h:74
void Reset()
Resets the timer.
Definition CaTimer.h:143
double GetAverage() const
Gets average time [s].
Definition CaTimer.h:59
int GetNofCalls() const
Gets number of calls.
Definition CaTimer.h:68
friend class boost::serialization::access
Definition CaTimer.h:92
std::chrono::time_point< Clock, Duration > TimePoint
Definition CaTimer.h:30
double GetMax() const
Gets time of the longest call [s].
Definition CaTimer.h:62
Duration::rep DurationCount
Definition CaTimer.h:29
DurationCount fMin
Minimal time period.
Definition CaTimer.h:105
~Timer()=default
Destructor.
TODO: SZh 8.11.2022: add selection of parameterisation.
Definition CaBranch.h:14