CoinUtils 2.11.4
CoinTime.hpp
Go to the documentation of this file.
1/* $Id$ */
2// Copyright (C) 2002, International Business Machines
3// Corporation and others. All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6#ifndef _CoinTime_hpp
7#define _CoinTime_hpp
8
9// Uncomment the next three lines for thorough memory initialisation.
10// #ifndef ZEROFAULT
11// # define ZEROFAULT
12// #endif
13
14//#############################################################################
15
16#include <ctime>
17#if defined(_MSC_VER)
18// Turn off compiler warning about long names
19#pragma warning(disable : 4786)
20#else
21// MacOS-X and FreeBSD needs sys/time.h
22#if defined(__MACH__) || defined(__FreeBSD__)
23#include <sys/time.h>
24#endif
25#if !defined(__MSVCRT__)
26#include <sys/resource.h>
27#endif
28#endif
29
30//#############################################################################
31
32#if defined(_MSC_VER)
33
34#if 0 // change this to 1 if want to use the win32 API
35#include <windows.h>
36#ifdef small
37/* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
38 '#define small char' */
39#undef small
40#endif
41#define TWO_TO_THE_THIRTYTWO 4294967296.0
42#define DELTA_EPOCH_IN_SECS 11644473600.0
43inline double CoinGetTimeOfDay()
44{
45 FILETIME ft;
46
47 GetSystemTimeAsFileTime(&ft);
48 double t = ft.dwHighDateTime * TWO_TO_THE_THIRTYTWO + ft.dwLowDateTime;
49 t = t/10000000.0 - DELTA_EPOCH_IN_SECS;
50 return t;
51}
52#else
53#include <sys/types.h>
54#include <sys/timeb.h>
55inline double CoinGetTimeOfDay()
56{
57 struct _timeb timebuffer;
58#pragma warning(disable : 4996)
59 _ftime(&timebuffer); // C4996
60#pragma warning(default : 4996)
61 return timebuffer.time + timebuffer.millitm / 1000.0;
62}
63#endif
64
65#else
66
67#include <sys/time.h>
68
69inline double CoinGetTimeOfDay()
70{
71 struct timeval tv;
72 gettimeofday(&tv, NULL);
73 return static_cast< double >(tv.tv_sec) + static_cast< int >(tv.tv_usec) / 1000000.0;
74}
75
76#endif // _MSC_VER
77
86inline double CoinWallclockTime(double callType = 0)
87{
88 double callTime = CoinGetTimeOfDay();
89 static const double firstCall = callType > 0 ? callType : callTime;
90 return callType < 0 ? firstCall : callTime - firstCall;
91}
92
93//#############################################################################
94
95//#define HAVE_SDK // if SDK under Win32 is installed, for CPU instead of elapsed time under Win
96#ifdef HAVE_SDK
97#include <windows.h>
98#ifdef small
99/* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
100 '#define small char' */
101#undef small
102#endif
103#define TWO_TO_THE_THIRTYTWO 4294967296.0
104#endif
105
106static inline double CoinCpuTime()
107{
108#ifdef COIN_DOING_DIFFS
109 // when trying to see differences between runs it can be helpful
110 return 0.0;
111#endif
112 double cpu_temp;
113#if defined(_MSC_VER) || defined(__MSVCRT__)
114#ifdef HAVE_SDK
115 FILETIME creation;
116 FILETIME exit;
117 FILETIME kernel;
118 FILETIME user;
119 GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user);
120 double t = user.dwHighDateTime * TWO_TO_THE_THIRTYTWO + user.dwLowDateTime;
121 return t / 10000000.0;
122#else
123 unsigned int ticksnow; /* clock_t is same as int */
124 ticksnow = (unsigned int)clock();
125 cpu_temp = (double)((double)ticksnow / CLOCKS_PER_SEC);
126#endif
127
128#else
129 struct rusage usage;
130#ifdef ZEROFAULT
131 usage.ru_utime.tv_sec = 0;
132 usage.ru_utime.tv_usec = 0;
133#endif
134 getrusage(RUSAGE_SELF, &usage);
135 cpu_temp = static_cast< double >(usage.ru_utime.tv_sec);
136 cpu_temp += 1.0e-6 * (static_cast< double >(usage.ru_utime.tv_usec));
137#endif
138 return cpu_temp;
139}
140
141//#############################################################################
142
143static inline double CoinSysTime()
144{
145 double sys_temp;
146#if defined(_MSC_VER) || defined(__MSVCRT__)
147 sys_temp = 0.0;
148#else
149 struct rusage usage;
150#ifdef ZEROFAULT
151 usage.ru_utime.tv_sec = 0;
152 usage.ru_utime.tv_usec = 0;
153#endif
154 getrusage(RUSAGE_SELF, &usage);
155 sys_temp = static_cast< double >(usage.ru_stime.tv_sec);
156 sys_temp += 1.0e-6 * (static_cast< double >(usage.ru_stime.tv_usec));
157#endif
158 return sys_temp;
159}
160
161//#############################################################################
162// On most systems SELF seems to include children threads, This is for when it doesn't
163static inline double CoinCpuTimeJustChildren()
164{
165 double cpu_temp;
166#if defined(_MSC_VER) || defined(__MSVCRT__)
167 cpu_temp = 0.0;
168#else
169 struct rusage usage;
170#ifdef ZEROFAULT
171 usage.ru_utime.tv_sec = 0;
172 usage.ru_utime.tv_usec = 0;
173#endif
174 getrusage(RUSAGE_CHILDREN, &usage);
175 cpu_temp = static_cast< double >(usage.ru_utime.tv_sec);
176 cpu_temp += 1.0e-6 * (static_cast< double >(usage.ru_utime.tv_usec));
177#endif
178 return cpu_temp;
179}
180//#############################################################################
181
182#include <fstream>
183
200private:
202 double start;
204 double limit;
205 double end;
206#ifdef COIN_COMPILE_WITH_TRACING
207 std::fstream *stream;
208 bool write_stream;
209#endif
210
211private:
212#ifdef COIN_COMPILE_WITH_TRACING
213 inline bool evaluate(bool b_tmp) const
214 {
215 int i_tmp = b_tmp;
216 if (stream) {
217 if (write_stream)
218 (*stream) << i_tmp << "\n";
219 else
220 (*stream) >> i_tmp;
221 }
222 return i_tmp;
223 }
224 inline double evaluate(double d_tmp) const
225 {
226 if (stream) {
227 if (write_stream)
228 (*stream) << d_tmp << "\n";
229 else
230 (*stream) >> d_tmp;
231 }
232 return d_tmp;
233 }
234#else
235 inline bool evaluate(const bool b_tmp) const
236 {
237 return b_tmp;
238 }
239 inline double evaluate(const double d_tmp) const
240 {
241 return d_tmp;
242 }
243#endif
244
245public:
248 : start(0)
249 , limit(1e100)
250 , end(1e100)
251#ifdef COIN_COMPILE_WITH_TRACING
252 , stream(0)
253 , write_stream(true)
254#endif
255 {
256 }
257
259 CoinTimer(double lim)
260 : start(CoinCpuTime())
261 , limit(lim)
262 , end(start + lim)
263#ifdef COIN_COMPILE_WITH_TRACING
264 , stream(0)
265 , write_stream(true)
266#endif
267 {
268 }
269
270#ifdef COIN_COMPILE_WITH_TRACING
273 CoinTimer(std::fstream *s, bool write)
274 : start(0)
275 , limit(1e100)
276 , end(1e100)
277 , stream(s)
278 , write_stream(write)
279 {
280 }
281
284 CoinTimer(double lim, std::fstream *s, bool w)
285 : start(CoinCpuTime())
286 , limit(lim)
287 , end(start + lim)
288 , stream(s)
289 , write_stream(w)
290 {
291 }
292#endif
293
295 inline void restart()
296 {
297 start = CoinCpuTime();
298 end = start + limit;
299 }
301 inline void reset() { restart(); }
303 inline void reset(double lim)
304 {
305 limit = lim;
306 restart();
307 }
308
311 inline bool isPastPercent(double pct) const
312 {
313 return evaluate(start + limit * pct < CoinCpuTime());
314 }
317 inline bool isPast(double lim) const
318 {
319 return evaluate(start + lim < CoinCpuTime());
320 }
323 inline bool isExpired() const
324 {
325 return evaluate(end < CoinCpuTime());
326 }
327
329 inline double timeLeft() const
330 {
331 return evaluate(end - CoinCpuTime());
332 }
333
335 inline double timeElapsed() const
336 {
337 return evaluate(CoinCpuTime() - start);
338 }
339
340 inline void setLimit(double l)
341 {
342 limit = l;
343 return;
344 }
345};
346
347#endif
348
349/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
350*/
static double CoinCpuTimeJustChildren()
Definition: CoinTime.hpp:163
double CoinGetTimeOfDay()
Definition: CoinTime.hpp:69
static double CoinSysTime()
Definition: CoinTime.hpp:143
double CoinWallclockTime(double callType=0)
Query the elapsed wallclock time since the first call to this function.
Definition: CoinTime.hpp:86
static double CoinCpuTime()
Definition: CoinTime.hpp:106
This class implements a timer that also implements a tracing functionality.
Definition: CoinTime.hpp:199
void reset(double lim)
Reset (and restart) the timer and change its time limit.
Definition: CoinTime.hpp:303
double evaluate(const double d_tmp) const
Definition: CoinTime.hpp:239
bool evaluate(const bool b_tmp) const
Definition: CoinTime.hpp:235
double timeLeft() const
Return how much time is left on the timer.
Definition: CoinTime.hpp:329
void setLimit(double l)
Definition: CoinTime.hpp:340
CoinTimer()
Default constructor creates a timer with no time limit and no tracing.
Definition: CoinTime.hpp:247
double limit
Definition: CoinTime.hpp:204
bool isExpired() const
Return whether the originally specified time limit has passed since the timer was started.
Definition: CoinTime.hpp:323
bool isPast(double lim) const
Return whether the given amount of time has elapsed since the timer was started.
Definition: CoinTime.hpp:317
double start
When the timer was initialized/reset/restarted.
Definition: CoinTime.hpp:202
double timeElapsed() const
Return how much time has elapsed.
Definition: CoinTime.hpp:335
CoinTimer(double lim)
Create a timer with the given time limit and with no tracing.
Definition: CoinTime.hpp:259
double end
Definition: CoinTime.hpp:205
void reset()
An alternate name for restart()
Definition: CoinTime.hpp:301
bool isPastPercent(double pct) const
Return whether the given percentage of the time limit has elapsed since the timer was started.
Definition: CoinTime.hpp:311
void restart()
Restart the timer (keeping the same time limit)
Definition: CoinTime.hpp:295