Jpp
21.0.1
the software that should make you happy
Loading...
Searching...
No Matches
software
JLang
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
16
namespace
JLANG
{}
17
namespace
JPP
{
using namespace
JLANG
; }
18
19
namespace
JLANG
{
20
21
/**
22
* Auxiliary class for CPU timing.
23
*/
24
class
JClock
:
25
public
JTitle
26
{
27
public
:
28
/**
29
* Default constructor.
30
*/
31
JClock
() :
32
JTitle
(),
33
ts
(0),
34
count
(0)
35
{}
36
37
38
/**
39
* Constructor.
40
*
41
* \param title title
42
*/
43
JClock
(
const
JTitle
&
title
) :
44
JTitle
(
title
),
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
*/
59
sentry
(
JClock
&
clock
) :
60
clock
(
clock
)
61
{
62
clock
.
start
();
63
}
64
65
/**
66
* Destructor.
67
*/
68
~sentry
()
69
{
70
clock
.
stop
();
71
}
72
73
private
:
74
JClock
&
clock
;
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
{
166
typedef
std::map<T, JClock>
map_type
;
167
168
/**
169
* Destructor.
170
*
171
* The clock data will be printed to the set output stream.
172
*/
173
~JClockwork
()
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
JTitle.hh
JLANG::JClock
Auxiliary class for CPU timing.
Definition
JClock.hh:26
JLANG::JClock::t1
std::chrono::high_resolution_clock::time_point t1
Definition
JClock.hh:153
JLANG::JClock::stop
void stop()
Stop clock.
Definition
JClock.hh:100
JLANG::JClock::count
size_t count
Definition
JClock.hh:155
JLANG::JClock::reset
void reset()
Reset clock.
Definition
JClock.hh:81
JLANG::JClock::start
void start()
Start clock.
Definition
JClock.hh:91
JLANG::JClock::ts
std::chrono::high_resolution_clock::duration ts
Definition
JClock.hh:154
JLANG::JClock::JClock
JClock(const JTitle &title)
Constructor.
Definition
JClock.hh:43
JLANG::JClock::JClock
JClock()
Default constructor.
Definition
JClock.hh:31
JLANG::JClock::operator<<
friend std::ostream & operator<<(std::ostream &out, const JClock &clock)
Print clock data.
Definition
JClock.hh:143
JLANG::JClock::t0
std::chrono::high_resolution_clock::time_point t0
Definition
JClock.hh:152
JLANG::JClock::print
void print(std::ostream &out) const
Print clock data.
Definition
JClock.hh:126
JLANG::JClock::operator()
size_t operator()() const
Get elapsed time.
Definition
JClock.hh:115
JLANG::JTitle
Auxiliary class for title.
Definition
JTitle.hh:19
JLANG::JTitle::title
std::string title
Definition
JTitle.hh:73
JLANG::JTitle::getTitle
const std::string & getTitle() const
Get title.
Definition
JTitle.hh:55
std::map
Definition
JSTDTypes.hh:19
JLANG
Auxiliary classes and methods for language specific functionality.
Definition
JAbstractClass.hh:11
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition
JAAnetToolkit.hh:49
std
Definition
JSTDTypes.hh:14
JLANG::JClock::sentry
Auxiliary data structure for scoped timing.
Definition
JClock.hh:53
JLANG::JClock::sentry::clock
JClock & clock
Definition
JClock.hh:74
JLANG::JClock::sentry::sentry
sentry(JClock &clock)
Constructor.
Definition
JClock.hh:59
JLANG::JClock::sentry::~sentry
~sentry()
Destructor.
Definition
JClock.hh:68
JLANG::JClockwork
Group of clocks.
Definition
JClock.hh:165
JLANG::JClockwork::sets
void sets(std::ostream &out)
Set output stream.
Definition
JClock.hh:188
JLANG::JClockwork::out
std::ostream * out
Definition
JClock.hh:203
JLANG::JClockwork::map_type
std::map< T, JClock > map_type
Definition
JClock.hh:166
JLANG::JClockwork::sets
void sets()
Reset output stream.
Definition
JClock.hh:197
JLANG::JClockwork::~JClockwork
~JClockwork()
Destructor.
Definition
JClock.hh:173
Generated by
1.12.0