Jpp 21.0.1
the software that should make you happy
Loading...
Searching...
No Matches
JClock.hh
Go to the documentation of this file.
1#ifndef __JLANG__JCLOCK__
2#define __JLANG__JCLOCK__
3
4#include <chrono>
5#include <ostream>
6#include <iomanip>
7#include <map>
8
9#include "JLang/JTitle.hh"
10
11
12/**
13 * \author mdejong
14 */
15
16namespace JLANG {}
17namespace JPP { using namespace JLANG; }
18
19namespace JLANG {
20
21 /**
22 * Auxiliary class for CPU timing.
23 */
24 class JClock :
25 public JTitle
26 {
27 public:
28 /**
29 * Default constructor.
30 */
32 JTitle(),
33 ts(0),
34 count(0)
35 {}
36
37
38 /**
39 * Constructor.
40 *
41 * \param title title
42 */
45 ts(0),
46 count(0)
47 {}
48
49
50 /**
51 * Auxiliary data structure for scoped timing.
52 */
53 struct sentry {
54 /**
55 * Constructor.
56 *
57 * \param clock clock
58 */
61 {
62 clock.start();
63 }
64
65 /**
66 * Destructor.
67 */
69 {
70 clock.stop();
71 }
72
73 private:
75 };
76
77
78 /**
79 * Reset clock.
80 */
81 void reset()
82 {
83 ts = std::chrono::high_resolution_clock::duration(0);
84 count = 0;
85 }
86
87
88 /**
89 * Start clock.
90 */
91 void start()
92 {
93 t0 = std::chrono::high_resolution_clock::now();
94 }
95
96
97 /**
98 * Stop clock.
99 */
100 void stop()
101 {
102 t1 = std::chrono::high_resolution_clock::now();
103
104 ts += (t1 - t0);
105
106 ++count;
107 }
108
109
110 /**
111 * Get elapsed time.
112 *
113 * \return elapsed time [ns]
114 */
115 size_t operator()() const
116 {
117 return ts / std::chrono::nanoseconds(1);
118 }
119
120
121 /**
122 * Print clock data.
123 *
124 * \param out output stream
125 */
126 void print(std::ostream& out) const
127 {
128 using namespace std;
129
130 out << setw(16) << left << getTitle() << ' '
131 << setw(12) << right << count << ' '
132 << setw(12) << ts / std::chrono::milliseconds(1) << " [ms]";
133 }
134
135
136 /**
137 * Print clock data.
138 *
139 * \param out output stream
140 * \param clock clock
141 * \return output stream
142 */
143 friend inline std::ostream& operator<<(std::ostream& out, const JClock& clock)
144 {
145 clock.print(out);
146
147 return out;
148 }
149
150
151 protected:
152 std::chrono::high_resolution_clock::time_point t0;
153 std::chrono::high_resolution_clock::time_point t1;
154 std::chrono::high_resolution_clock::duration ts;
155 size_t count;
156 };
157
158
159 /**
160 * Group of clocks.
161 */
162 template<class T>
163 struct JClockwork :
164 public std::map<T, JClock>
165 {
167
168 /**
169 * Destructor.
170 *
171 * The clock data will be printed to the set output stream.
172 */
174 {
175 if (out != NULL) {
176 for (typename map_type::const_iterator i = this->begin(); i != this->end(); ++i) {
177 *out << i->first << ' ' << i->second << std::endl;
178 }
179 }
180 }
181
182
183 /**
184 * Set output stream.
185 *
186 * \param out output stream
187 */
188 void sets(std::ostream& out)
189 {
190 this->out = &out;
191 }
192
193
194 /**
195 * Reset output stream.
196 */
197 void sets()
198 {
199 this->out = NULL;
200 }
201
202 protected:
203 std::ostream* out = NULL;
204 };
205}
206
207#endif
Auxiliary class for CPU timing.
Definition JClock.hh:26
std::chrono::high_resolution_clock::time_point t1
Definition JClock.hh:153
void stop()
Stop clock.
Definition JClock.hh:100
size_t count
Definition JClock.hh:155
void reset()
Reset clock.
Definition JClock.hh:81
void start()
Start clock.
Definition JClock.hh:91
std::chrono::high_resolution_clock::duration ts
Definition JClock.hh:154
JClock(const JTitle &title)
Constructor.
Definition JClock.hh:43
JClock()
Default constructor.
Definition JClock.hh:31
friend std::ostream & operator<<(std::ostream &out, const JClock &clock)
Print clock data.
Definition JClock.hh:143
std::chrono::high_resolution_clock::time_point t0
Definition JClock.hh:152
void print(std::ostream &out) const
Print clock data.
Definition JClock.hh:126
size_t operator()() const
Get elapsed time.
Definition JClock.hh:115
Auxiliary class for title.
Definition JTitle.hh:19
std::string title
Definition JTitle.hh:73
const std::string & getTitle() const
Get title.
Definition JTitle.hh:55
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for scoped timing.
Definition JClock.hh:53
sentry(JClock &clock)
Constructor.
Definition JClock.hh:59
~sentry()
Destructor.
Definition JClock.hh:68
Group of clocks.
Definition JClock.hh:165
void sets(std::ostream &out)
Set output stream.
Definition JClock.hh:188
std::ostream * out
Definition JClock.hh:203
std::map< T, JClock > map_type
Definition JClock.hh:166
void sets()
Reset output stream.
Definition JClock.hh:197
~JClockwork()
Destructor.
Definition JClock.hh:173