... | ... |
@@ -77,257 +77,257 @@ |
77 | 77 |
gettimeofday(&tv, 0); |
78 | 78 |
rtime=tv.tv_sec+double(tv.tv_usec)/1e6; |
79 | 79 |
|
80 | 80 |
tms ts; |
81 | 81 |
double tck=sysconf(_SC_CLK_TCK); |
82 | 82 |
times(&ts); |
83 | 83 |
utime=ts.tms_utime/tck; |
84 | 84 |
stime=ts.tms_stime/tck; |
85 | 85 |
cutime=ts.tms_cutime/tck; |
86 | 86 |
cstime=ts.tms_cstime/tck; |
87 | 87 |
#else |
88 | 88 |
bits::getWinProcTimes(rtime, utime, stime, cutime, cstime); |
89 | 89 |
#endif |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
/// Constructor initializing with zero |
93 | 93 |
TimeStamp() |
94 | 94 |
{ _reset(); } |
95 | 95 |
///Constructor initializing with the current time values of the process |
96 | 96 |
TimeStamp(void *) { stamp();} |
97 | 97 |
|
98 | 98 |
///Set every time value to zero |
99 | 99 |
TimeStamp &reset() {_reset();return *this;} |
100 | 100 |
|
101 | 101 |
///\e |
102 | 102 |
TimeStamp &operator+=(const TimeStamp &b) |
103 | 103 |
{ |
104 | 104 |
utime+=b.utime; |
105 | 105 |
stime+=b.stime; |
106 | 106 |
cutime+=b.cutime; |
107 | 107 |
cstime+=b.cstime; |
108 | 108 |
rtime+=b.rtime; |
109 | 109 |
return *this; |
110 | 110 |
} |
111 | 111 |
///\e |
112 | 112 |
TimeStamp operator+(const TimeStamp &b) const |
113 | 113 |
{ |
114 | 114 |
TimeStamp t(*this); |
115 | 115 |
return t+=b; |
116 | 116 |
} |
117 | 117 |
///\e |
118 | 118 |
TimeStamp &operator-=(const TimeStamp &b) |
119 | 119 |
{ |
120 | 120 |
utime-=b.utime; |
121 | 121 |
stime-=b.stime; |
122 | 122 |
cutime-=b.cutime; |
123 | 123 |
cstime-=b.cstime; |
124 | 124 |
rtime-=b.rtime; |
125 | 125 |
return *this; |
126 | 126 |
} |
127 | 127 |
///\e |
128 | 128 |
TimeStamp operator-(const TimeStamp &b) const |
129 | 129 |
{ |
130 | 130 |
TimeStamp t(*this); |
131 | 131 |
return t-=b; |
132 | 132 |
} |
133 | 133 |
///\e |
134 | 134 |
TimeStamp &operator*=(double b) |
135 | 135 |
{ |
136 | 136 |
utime*=b; |
137 | 137 |
stime*=b; |
138 | 138 |
cutime*=b; |
139 | 139 |
cstime*=b; |
140 | 140 |
rtime*=b; |
141 | 141 |
return *this; |
142 | 142 |
} |
143 | 143 |
///\e |
144 | 144 |
TimeStamp operator*(double b) const |
145 | 145 |
{ |
146 | 146 |
TimeStamp t(*this); |
147 | 147 |
return t*=b; |
148 | 148 |
} |
149 | 149 |
friend TimeStamp operator*(double b,const TimeStamp &t); |
150 | 150 |
///\e |
151 | 151 |
TimeStamp &operator/=(double b) |
152 | 152 |
{ |
153 | 153 |
utime/=b; |
154 | 154 |
stime/=b; |
155 | 155 |
cutime/=b; |
156 | 156 |
cstime/=b; |
157 | 157 |
rtime/=b; |
158 | 158 |
return *this; |
159 | 159 |
} |
160 | 160 |
///\e |
161 | 161 |
TimeStamp operator/(double b) const |
162 | 162 |
{ |
163 | 163 |
TimeStamp t(*this); |
164 | 164 |
return t/=b; |
165 | 165 |
} |
166 | 166 |
///The time ellapsed since the last call of stamp() |
167 | 167 |
TimeStamp ellapsed() const |
168 | 168 |
{ |
169 | 169 |
TimeStamp t(NULL); |
170 | 170 |
return t-*this; |
171 | 171 |
} |
172 | 172 |
|
173 | 173 |
friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
174 | 174 |
|
175 | 175 |
///Gives back the user time of the process |
176 | 176 |
double userTime() const |
177 | 177 |
{ |
178 | 178 |
return utime; |
179 | 179 |
} |
180 | 180 |
///Gives back the system time of the process |
181 | 181 |
double systemTime() const |
182 | 182 |
{ |
183 | 183 |
return stime; |
184 | 184 |
} |
185 | 185 |
///Gives back the user time of the process' children |
186 | 186 |
|
187 | 187 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
188 | 188 |
/// |
189 | 189 |
double cUserTime() const |
190 | 190 |
{ |
191 | 191 |
return cutime; |
192 | 192 |
} |
193 | 193 |
///Gives back the user time of the process' children |
194 | 194 |
|
195 | 195 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
196 | 196 |
/// |
197 | 197 |
double cSystemTime() const |
198 | 198 |
{ |
199 | 199 |
return cstime; |
200 | 200 |
} |
201 | 201 |
///Gives back the real time |
202 | 202 |
double realTime() const {return rtime;} |
203 | 203 |
}; |
204 | 204 |
|
205 |
TimeStamp operator*(double b,const TimeStamp &t) |
|
205 |
inline TimeStamp operator*(double b,const TimeStamp &t) |
|
206 | 206 |
{ |
207 | 207 |
return t*b; |
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
///Prints the time counters |
211 | 211 |
|
212 | 212 |
///Prints the time counters in the following form: |
213 | 213 |
/// |
214 | 214 |
/// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt> |
215 | 215 |
/// |
216 | 216 |
/// where the values are the |
217 | 217 |
/// \li \c u: user cpu time, |
218 | 218 |
/// \li \c s: system cpu time, |
219 | 219 |
/// \li \c cu: user cpu time of children, |
220 | 220 |
/// \li \c cs: system cpu time of children, |
221 | 221 |
/// \li \c real: real time. |
222 | 222 |
/// \relates TimeStamp |
223 | 223 |
/// \note On <tt>WIN32</tt> platform the cummulative values are not |
224 | 224 |
/// calculated. |
225 | 225 |
inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
226 | 226 |
{ |
227 | 227 |
os << "u: " << t.userTime() << |
228 | 228 |
"s, s: " << t.systemTime() << |
229 | 229 |
"s, cu: " << t.cUserTime() << |
230 | 230 |
"s, cs: " << t.cSystemTime() << |
231 | 231 |
"s, real: " << t.realTime() << "s"; |
232 | 232 |
return os; |
233 | 233 |
} |
234 | 234 |
|
235 | 235 |
///Class for measuring the cpu time and real time usage of the process |
236 | 236 |
|
237 | 237 |
///Class for measuring the cpu time and real time usage of the process. |
238 | 238 |
///It is quite easy-to-use, here is a short example. |
239 | 239 |
///\code |
240 | 240 |
/// #include<lemon/time_measure.h> |
241 | 241 |
/// #include<iostream> |
242 | 242 |
/// |
243 | 243 |
/// int main() |
244 | 244 |
/// { |
245 | 245 |
/// |
246 | 246 |
/// ... |
247 | 247 |
/// |
248 | 248 |
/// Timer t; |
249 | 249 |
/// doSomething(); |
250 | 250 |
/// std::cout << t << '\n'; |
251 | 251 |
/// t.restart(); |
252 | 252 |
/// doSomethingElse(); |
253 | 253 |
/// std::cout << t << '\n'; |
254 | 254 |
/// |
255 | 255 |
/// ... |
256 | 256 |
/// |
257 | 257 |
/// } |
258 | 258 |
///\endcode |
259 | 259 |
/// |
260 | 260 |
///The \ref Timer can also be \ref stop() "stopped" and |
261 | 261 |
///\ref start() "started" again, so it is possible to compute collected |
262 | 262 |
///running times. |
263 | 263 |
/// |
264 | 264 |
///\warning Depending on the operation system and its actual configuration |
265 | 265 |
///the time counters have a certain (10ms on a typical Linux system) |
266 | 266 |
///granularity. |
267 | 267 |
///Therefore this tool is not appropriate to measure very short times. |
268 | 268 |
///Also, if you start and stop the timer very frequently, it could lead to |
269 | 269 |
///distorted results. |
270 | 270 |
/// |
271 | 271 |
///\note If you want to measure the running time of the execution of a certain |
272 | 272 |
///function, consider the usage of \ref TimeReport instead. |
273 | 273 |
/// |
274 | 274 |
///\sa TimeReport |
275 | 275 |
class Timer |
276 | 276 |
{ |
277 | 277 |
int _running; //Timer is running iff _running>0; (_running>=0 always holds) |
278 | 278 |
TimeStamp start_time; //This is the relativ start-time if the timer |
279 | 279 |
//is _running, the collected _running time otherwise. |
280 | 280 |
|
281 | 281 |
void _reset() {if(_running) start_time.stamp(); else start_time.reset();} |
282 | 282 |
|
283 | 283 |
public: |
284 | 284 |
///Constructor. |
285 | 285 |
|
286 | 286 |
///\param run indicates whether or not the timer starts immediately. |
287 | 287 |
/// |
288 | 288 |
Timer(bool run=true) :_running(run) {_reset();} |
289 | 289 |
|
290 | 290 |
///\name Control the state of the timer |
291 | 291 |
///Basically a Timer can be either running or stopped, |
292 | 292 |
///but it provides a bit finer control on the execution. |
293 | 293 |
///The \ref lemon::Timer "Timer" also counts the number of |
294 | 294 |
///\ref lemon::Timer::start() "start()" executions, and it stops |
295 | 295 |
///only after the same amount (or more) \ref lemon::Timer::stop() |
296 | 296 |
///"stop()"s. This can be useful e.g. to compute the running time |
297 | 297 |
///of recursive functions. |
298 | 298 |
|
299 | 299 |
///@{ |
300 | 300 |
|
301 | 301 |
///Reset and stop the time counters |
302 | 302 |
|
303 | 303 |
///This function resets and stops the time counters |
304 | 304 |
///\sa restart() |
305 | 305 |
void reset() |
306 | 306 |
{ |
307 | 307 |
_running=0; |
308 | 308 |
_reset(); |
309 | 309 |
} |
310 | 310 |
|
311 | 311 |
///Start the time counters |
312 | 312 |
|
313 | 313 |
///This function starts the time counters. |
314 | 314 |
/// |
315 | 315 |
///If the timer is started more than ones, it will remain running |
316 | 316 |
///until the same amount of \ref stop() is called. |
317 | 317 |
///\sa stop() |
318 | 318 |
void start() |
319 | 319 |
{ |
320 | 320 |
if(_running) _running++; |
321 | 321 |
else { |
322 | 322 |
_running=1; |
323 | 323 |
TimeStamp t; |
324 | 324 |
t.stamp(); |
325 | 325 |
start_time=t-start_time; |
326 | 326 |
} |
327 | 327 |
} |
328 | 328 |
|
329 | 329 |
|
330 | 330 |
///Stop the time counters |
331 | 331 |
|
332 | 332 |
///This function stops the time counters. If start() was executed more than |
333 | 333 |
///once, then the same number of stop() execution is necessary the really |
0 comments (0 inline)