Jpp
21.0.0-rc.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
8
#include "
JLang/JTitle.hh
"
9
10
11
/**
12
* \author mdejong
13
*/
14
15
namespace
JLANG
{}
16
namespace
JPP
{
using namespace
JLANG
; }
17
18
namespace
JLANG
{
19
20
/**
21
* Auxiliary class for CPU timing.
22
*/
23
class
JClock
:
24
public
JTitle
25
{
26
public
:
27
/**
28
* Default constructor.
29
*/
30
JClock
() :
31
JTitle
(),
32
ts
(0),
33
count
(0)
34
{}
35
36
37
/**
38
* Constructor.
39
*
40
* \param title title
41
*/
42
JClock
(
const
JTitle
&
title
) :
43
JTitle
(
title
),
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
*/
54
~JClock
()
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
*/
71
sentry
(
JClock
&
clock
) :
72
clock
(
clock
)
73
{
74
clock
.
start
();
75
}
76
77
/**
78
* Destructor.
79
*/
80
~sentry
()
81
{
82
clock
.
stop
();
83
}
84
85
private
:
86
JClock
&
clock
;
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
JTitle.hh
JLANG::JClock
Auxiliary class for CPU timing.
Definition
JClock.hh:25
JLANG::JClock::t1
std::chrono::high_resolution_clock::time_point t1
Definition
JClock.hh:177
JLANG::JClock::stop
void stop()
Stop clock.
Definition
JClock.hh:112
JLANG::JClock::count
size_t count
Definition
JClock.hh:179
JLANG::JClock::reset
void reset()
Reset clock.
Definition
JClock.hh:93
JLANG::JClock::start
void start()
Start clock.
Definition
JClock.hh:103
JLANG::JClock::ts
std::chrono::high_resolution_clock::duration ts
Definition
JClock.hh:178
JLANG::JClock::JClock
JClock(const JTitle &title)
Constructor.
Definition
JClock.hh:42
JLANG::JClock::JClock
JClock()
Default constructor.
Definition
JClock.hh:30
JLANG::JClock::operator<<
friend std::ostream & operator<<(std::ostream &out, const JClock &clock)
Print clock data.
Definition
JClock.hh:155
JLANG::JClock::gets
static std::ostream *& gets()
Get output stream.
Definition
JClock.hh:186
JLANG::JClock::t0
std::chrono::high_resolution_clock::time_point t0
Definition
JClock.hh:176
JLANG::JClock::print
void print(std::ostream &out) const
Print clock data.
Definition
JClock.hh:138
JLANG::JClock::sets
static void sets(std::ostream &out)
Set output stream.
Definition
JClock.hh:168
JLANG::JClock::~JClock
~JClock()
Destructor.
Definition
JClock.hh:54
JLANG::JClock::operator()
size_t operator()() const
Get elapsed time.
Definition
JClock.hh:127
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
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:48
std
Definition
JSTDTypes.hh:14
JLANG::JClock::sentry
Auxiliary data structure for scoped timing.
Definition
JClock.hh:65
JLANG::JClock::sentry::clock
JClock & clock
Definition
JClock.hh:86
JLANG::JClock::sentry::sentry
sentry(JClock &clock)
Constructor.
Definition
JClock.hh:71
JLANG::JClock::sentry::~sentry
~sentry()
Destructor.
Definition
JClock.hh:80
Generated by
1.12.0