netxsimdg
Loading...
Searching...
No Matches
Chrono.hpp
Go to the documentation of this file.
1
8#ifndef SRC_INCLUDE_CHRONO_HPP_
9#define SRC_INCLUDE_CHRONO_HPP_
10
11#include <chrono>
12#include <ctime>
13
14namespace Nextsim {
21class Chrono {
22public:
24 typedef std::chrono::high_resolution_clock::time_point WallTimePoint;
26 typedef std::chrono::high_resolution_clock::duration WallTimeDuration;
27
29 typedef std::clock_t CpuTimePoint;
31 typedef double CpuTimeDuration;
32
35 : m_wallTime(WallTimeDuration::zero())
36 , m_cpuTime(0)
37 , m_ticks(0)
38 , m_running(false) {};
39 ~Chrono() = default;
40
42 inline WallTimePoint wallHack() const { return m_wallHack; };
45 {
46 return m_wallTime + (m_running ? wallTimeSinceHack() : WallTimeDuration::zero());
47 };
48
50 inline void reset()
51 {
52 m_wallTime = WallTimeDuration::zero();
53 m_cpuTime = 0;
54 m_ticks = 0;
55 m_running = false;
56 }
57
59 inline CpuTimePoint cpuHack() const { return m_cpuHack; };
61 inline CpuTimeDuration cpuTime() const
62 {
63 return m_cpuTime + (m_running ? cpuTimeSinceHack() : 0);
64 };
65
67 inline int ticks() const { return m_ticks; };
69 inline bool running() const { return m_running; };
70
77 inline void start()
78 {
79 m_wallHack = std::chrono::high_resolution_clock::now();
80 m_cpuHack = std::clock();
81 ++m_ticks;
82 m_running = true;
83 };
84
91 void stop()
92 {
93 m_wallTime += wallTimeSinceHack();
94 m_cpuTime += cpuTimeSinceHack();
95 m_running = false;
96 };
97
103 inline void extraCpuTime(const CpuTimeDuration& extraTime) { m_cpuTime += extraTime; };
109 inline void extraWallTime(const WallTimeDuration& extraTime) { m_wallTime += extraTime; };
116 inline void extraTicks(int extraTicks) { m_ticks += extraTicks; };
117
118private:
119 WallTimePoint m_wallHack;
120 WallTimeDuration m_wallTime;
121
122 CpuTimePoint m_cpuHack;
123 CpuTimeDuration m_cpuTime;
124
125 int m_ticks;
126
127 bool m_running;
128
129 CpuTimeDuration cpuTimeSinceHack() const
130 {
131 return static_cast<CpuTimeDuration>(std::clock() - m_cpuHack) / CLOCKS_PER_SEC;
132 }
133
134 WallTimeDuration wallTimeSinceHack() const
135 {
136 return std::chrono::duration_cast<WallTimeDuration>(
137 std::chrono::high_resolution_clock::now() - m_wallHack);
138 }
139};
140
141} /* namespace Nextsim */
142
143#endif /* SRC_INCLUDE_CHRONO_HPP_ */
A class providing a timer.
Definition Chrono.hpp:21
void extraWallTime(const WallTimeDuration &extraTime)
Adds an externally determined increment to the wall clock.
Definition Chrono.hpp:109
std::chrono::high_resolution_clock::duration WallTimeDuration
Type of a time duration on the wall clock.
Definition Chrono.hpp:26
void stop()
Stops the timer.
Definition Chrono.hpp:91
WallTimeDuration wallTime() const
Returns the current cumulative wall clock time.
Definition Chrono.hpp:44
void reset()
Resets all of the chronometer counters.
Definition Chrono.hpp:50
int ticks() const
Returns the current number of activation ticks.
Definition Chrono.hpp:67
std::chrono::high_resolution_clock::time_point WallTimePoint
Type of a point in time for the wall clock.
Definition Chrono.hpp:24
std::clock_t CpuTimePoint
Type of a point in time for the CPU clock.
Definition Chrono.hpp:29
void extraCpuTime(const CpuTimeDuration &extraTime)
Adds an externally determined increment to the CPU clock.
Definition Chrono.hpp:103
CpuTimeDuration cpuTime() const
Returns the current cumulative CPU clock timer.
Definition Chrono.hpp:61
WallTimePoint wallHack() const
Returns the current time on the wall clock.
Definition Chrono.hpp:42
CpuTimePoint cpuHack() const
Returns the current time on the CPU clock.
Definition Chrono.hpp:59
Chrono()
Default constructor of a chronometer, zeroing all of the counters.
Definition Chrono.hpp:34
double CpuTimeDuration
Type of a time duration on the CPU clock.
Definition Chrono.hpp:31
void extraTicks(int extraTicks)
Adds an externally determined increment to the activation count.
Definition Chrono.hpp:116
void start()
Starts the timer.
Definition Chrono.hpp:77
bool running() const
Returns whether this chronometer is running.
Definition Chrono.hpp:69