netxsimdg
Loading...
Searching...
No Matches
Timer.hpp
Go to the documentation of this file.
1
7#ifndef SRC_INCLUDE_TIMER_HPP
8#define SRC_INCLUDE_TIMER_HPP
9
10#include "Chrono.hpp"
11
12#include <chrono>
13#include <ctime>
14#include <forward_list>
15#include <map>
16#include <ostream>
17#include <set>
18#include <string>
19
20namespace Nextsim {
21
23class Timer {
24public:
25 typedef std::string Key;
26
27 typedef Chrono::WallTimePoint WallTimePoint;
28 typedef Chrono::WallTimeDuration WallTimeDuration;
29 typedef Chrono::CpuTimePoint CpuTimePoint;
30 typedef Chrono::CpuTimeDuration CpuTimeDuration;
31
32 typedef std::forward_list<Key> TimerPath;
33
35 Timer();
41 Timer(const Key& rootKey);
42 virtual ~Timer() = default;
43
49 void tick(const Key& timerName);
55 void tock(const Key& timerName);
57 void tock();
58
64 double lap(const Key& timerName) const;
70 double elapsed(const Key& timerName) const;
71
78 std::ostream& report(const Key& timerName, std::ostream& os) const;
84 std::ostream& report(std::ostream& os) const;
91 std::ostream& report(const TimerPath&, std::ostream& os) const;
92
101 void additionalTime(const TimerPath& path, WallTimeDuration additionalWall,
102 CpuTimeDuration additionalCpu, int additionalTicks);
104 TimerPath currentTimerNodePath() const;
105
107 void reset();
109 static Timer main;
110
111private:
112 struct TimerNode {
113 TimerNode();
114 Key name;
115
116 Chrono timeKeeper;
117
118 std::map<Key, TimerNode> childNodes;
119 TimerNode* parent;
120
121 void tick();
122 void tock();
123 std::ostream& report(std::ostream& os, const std::string& prefix) const;
124 std::ostream& reportAll(std::ostream& os, const std::string& prefix) const;
125
126 TimerPath searchDescendants(const Key& timerName) const;
127 };
128
129 TimerPath pathToFirstMatch(const Key&) const;
130
131 TimerNode root;
132 TimerNode* current;
133};
134
135} /* namespace Nextsim */
136
138std::ostream& operator<<(std::ostream& os, const Nextsim::Timer& tim);
139
140#endif /* SRC_INCLUDE_TIMER_HPP */
std::ostream & operator<<(std::ostream &os, const Nextsim::Timer &tim)
Overload of the output operator for a Timer.
Definition Timer.cpp:201
A class providing a timer.
Definition Chrono.hpp:21
std::chrono::high_resolution_clock::duration WallTimeDuration
Type of a time duration on the wall clock.
Definition Chrono.hpp:26
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
double CpuTimeDuration
Type of a time duration on the CPU clock.
Definition Chrono.hpp:31
A class for a hierarchical timer functions.
Definition Timer.hpp:23
void additionalTime(const TimerPath &path, WallTimeDuration additionalWall, CpuTimeDuration additionalCpu, int additionalTicks)
Adds an additional time increment to a timer.
Definition Timer.cpp:65
double elapsed(const Key &timerName) const
Returns the elapsed time.
Definition Timer.cpp:63
void tock()
Stop the last timer to be started.
Definition Timer.cpp:50
std::ostream & report(const Key &timerName, std::ostream &os) const
Prints the status of a named timer to an ostream.
Definition Timer.cpp:94
void tick(const Key &timerName)
Starts a named timer.
Definition Timer.cpp:35
Timer()
Creates a Timer with an unnamed root node.
Definition Timer.cpp:22
double lap(const Key &timerName) const
Returns the elapsed time without stopping the timer.
Definition Timer.cpp:62
void reset()
Deletes all timers except the root, which is reset.
Definition Timer.cpp:110
TimerPath currentTimerNodePath() const
Returns the timer path to the currently running timer.
Definition Timer.cpp:78
static Timer main
Static timer for general use.
Definition Timer.hpp:109