Jpp 21.0.0-rc.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
8#include "JLang/JTitle.hh"
9
10
11/**
12 * \author mdejong
13 */
14
15namespace JLANG {}
16namespace JPP { using namespace JLANG; }
17
18namespace JLANG {
19
20 /**
21 * Auxiliary class for CPU timing.
22 */
23 class JClock :
24 public JTitle
25 {
26 public:
27 /**
28 * Default constructor.
29 */
31 JTitle(),
32 ts(0),
33 count(0)
34 {}
35
36
37 /**
38 * Constructor.
39 *
40 * \param title title
41 */
44 ts(0),
45 count(0)
46 {}
47
48
49 /**
50 * Destructor.
51 *
52 * The clock data will optionally be printed to the set output stream.
53 */
55 {
56 if (gets() != NULL && count != 0) {
57 *gets() << (*this) << std::endl;
58 }
59 }
60
61
62 /**
63 * Auxiliary data structure for scoped timing.
64 */
65 struct sentry {
66 /**
67 * Constructor.
68 *
69 * \param clock clock
70 */
73 {
74 clock.start();
75 }
76
77 /**
78 * Destructor.
79 */
81 {
82 clock.stop();
83 }
84
85 private:
87 };
88
89
90 /**
91 * Reset clock.
92 */
93 void reset()
94 {
95 ts = std::chrono::high_resolution_clock::duration(0);
96 count = 0;
97 }
98
99
100 /**
101 * Start clock.
102 */
103 void start()
104 {
105 t0 = std::chrono::high_resolution_clock::now();
106 }
107
108
109 /**
110 * Stop clock.
111 */
112 void stop()
113 {
114 t1 = std::chrono::high_resolution_clock::now();
115
116 ts += (t1 - t0);
117
118 ++count;
119 }
120
121
122 /**
123 * Get elapsed time.
124 *
125 * \return elapsed time [ns]
126 */
127 size_t operator()() const
128 {
129 return ts / std::chrono::nanoseconds(1);
130 }
131
132
133 /**
134 * Print clock data.
135 *
136 * \param out output stream
137 */
138 void print(std::ostream& out) const
139 {
140 using namespace std;
141
142 out << setw(16) << left << getTitle() << ' '
143 << setw(12) << right << count << ' '
144 << setw(12) << ts / std::chrono::milliseconds(1) << " [ms]";
145 }
146
147
148 /**
149 * Print clock data.
150 *
151 * \param out output stream
152 * \param clock clock
153 * \return output stream
154 */
155 friend inline std::ostream& operator<<(std::ostream& out, const JClock& clock)
156 {
157 clock.print(out);
158
159 return out;
160 }
161
162
163 /**
164 * Set output stream.
165 *
166 * \param out output stream
167 */
168 static void sets(std::ostream& out)
169 {
170 gets() = &out;
171 }
172
173
174 protected:
175
176 std::chrono::high_resolution_clock::time_point t0;
177 std::chrono::high_resolution_clock::time_point t1;
178 std::chrono::high_resolution_clock::duration ts;
179 size_t count;
180
181 /**
182 * Get output stream.
183 *
184 * \return output stream
185 */
186 static std::ostream*& gets()
187 {
188 static std::ostream* out = NULL;
189
190 return out;
191 }
192 };
193}
194
195#endif
Auxiliary class for CPU timing.
Definition JClock.hh:25
std::chrono::high_resolution_clock::time_point t1
Definition JClock.hh:177
void stop()
Stop clock.
Definition JClock.hh:112
size_t count
Definition JClock.hh:179
void reset()
Reset clock.
Definition JClock.hh:93
void start()
Start clock.
Definition JClock.hh:103
std::chrono::high_resolution_clock::duration ts
Definition JClock.hh:178
JClock(const JTitle &title)
Constructor.
Definition JClock.hh:42
JClock()
Default constructor.
Definition JClock.hh:30
friend std::ostream & operator<<(std::ostream &out, const JClock &clock)
Print clock data.
Definition JClock.hh:155
static std::ostream *& gets()
Get output stream.
Definition JClock.hh:186
std::chrono::high_resolution_clock::time_point t0
Definition JClock.hh:176
void print(std::ostream &out) const
Print clock data.
Definition JClock.hh:138
static void sets(std::ostream &out)
Set output stream.
Definition JClock.hh:168
~JClock()
Destructor.
Definition JClock.hh:54
size_t operator()() const
Get elapsed time.
Definition JClock.hh:127
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:65
sentry(JClock &clock)
Constructor.
Definition JClock.hh:71
~sentry()
Destructor.
Definition JClock.hh:80