TBCI Numerical high perf. C++ Library 2.8.0
stopwatch.h
Go to the documentation of this file.
1
18
19#ifndef TBCI_STOPWATCH_H
20#define TBCI_STOPWATCH_H
21
39
40/* We need to know about certain settings, but we don't want to include basics.h */
41#ifdef HAVE_TBCICONFIG_H
42# include "tbci/tbciconfig.h"
43#else
44# include "tbci/config_manual.h"
45#endif
46
47//#define _ANSI_C_SOURCE
48#include <time.h>
54#ifdef HAVE_UNISTD_H
55# include <unistd.h>
56#endif
57#if defined (__linux__) && !defined(CLK_TCK)
58# define CLK_TCK ((clock_t) sysconf (_SC_CLK_TCK))
59#endif
60
61#ifdef HAVE_LIMITS_H
62# include <limits.h>
63#else
64# ifndef LONG_MIN
65# if defined(__WORDSIZE) && __WORDSIZE == 64
66# define LONG_MIN (-9223372036854775807L - 1L)
67# else
68# define LONG_MIN (-2147483647L - 1L)
69# endif
70# endif
71#endif
72
73/* Maybe we use a Linux kernel with changed HZ ... */
74#if defined(__linux__) && defined(BROKEN_HZ) /* && defined(__i386__) */
75# include <asm/param.h>
76# define CPS (CLOCKS_PER_SEC*HZ/CLK_TCK)
77#else
78# define CPS CLOCKS_PER_SEC
79#endif
80
85
87{
88 protected:
90 double last_time;
92 double total;
94 const double secs_per_tick;
99 const double overflow_secs;
102
104 virtual double seconds() const
105 {
106 return ((double) clock()) * secs_per_tick;
107 }
108
110 {
111 const double secs = seconds();
112 double diff = secs - last_time;
113#ifdef __i386__ /* Extra precision is killing us on i386 */
114 if (diff < 0.0 && -diff < secs_per_tick/4)
115 diff = 0.0;
116#endif
117 if (overflow_secs != 0.0 && diff < 0.0)
118 diff += overflow_secs;
119
120 last_time = secs;
121 total += diff;
122 return diff;
123 }
124
125 public:
127 stopw_base(const double tick, const double over = -1.0)
128 : last_time(0.0)
129 , total(0.0)
130 , secs_per_tick (tick)
131 , overflow_secs (over == -1.0 ? -tick * (2.0 * LONG_MIN): over)
132 , running(0)
133 {}
134
135 virtual ~stopw_base() {}
137 double reset()
138 {
139 const double t = total;
140 last_time = 0.0;
141 total = 0.0; running = 0;
142 return t;
143 }
144
145 void start()
146 {
147 if (!running) {
148 running = 1; last_time = seconds();
149 } else
150 adv_stopwatch();
151 }
152
153 double stop()
154 {
155 if (running) {
156 running = 0;
158 }
159 return total;
160 }
161
162 double stop_d()
163 {
164 if (running) {
165 running = 0;
166 return adv_stopwatch();
167 } else
168 return 0.0;
169 }
170
173 double read()
174 {
175 if (running)
177 return total;
178 }
179
181 double read_d()
182 {
183 if (running)
184 return adv_stopwatch();
185 else
186 return 0.0;
187 }
188};
189
193class stopwatch : public stopw_base
194{
195 public:
197};
198
199#ifndef unix
200typedef stopwatch stopwatch_u;
201typedef stopwatch stopwatch_us;
202#else
203//#ifdef HAVE_SYS_TIMES_H
204#include <sys/times.h>
205//#endif
211{
212 protected:
213 virtual double seconds() const
214 {
215 struct tms tims;
216 /*if (*/times (&tims)/* < 0) STD__ cerr << "Error reading time!" << STD__ endl*/;
217 return (double)(tims.tms_utime+tims.tms_cutime) * secs_per_tick;
218 }
219
220 public:
221 stopwatch_u() : stopw_base(1000000.0 / (CPS * CLK_TCK)) {}
222};
223
229{
230 protected:
231 virtual double seconds() const
232 {
233 struct tms tims;
234 /*if (*/times (&tims)/* < 0) STD__ cerr << "Error reading time!" << STD__ endl*/;
235 return (double)(tims.tms_utime+tims.tms_cutime+tims.tms_stime+tims.tms_cstime)
237 }
238
239 public:
240 stopwatch_us() : stopw_base(1000000.0 / (CPS * CLK_TCK)) {}
241};
242
243#endif /* unix */
244
249
250#ifdef unix
251//#ifdef HAVE_SYS_TIME_H
252#include <sys/time.h>
253//#endif
254
255//#ifdef HAVE_GETTIMEOFDAY
257{
258 protected:
259 double seconds() const
260 {
261 struct timeval tv;
262 gettimeofday(&tv, NULL);
263 return (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
264 }
265
266 public:
267 stopwatch_e() : stopw_base(0.000001, 0) {}
268};
269//#endif /* HAVE_GETTIMEOFDAY */
270
271#else /* unix */
272# ifdef _MSC_VER
273
274#include <sys/timeb.h>
275
276class stopwatch_e : public stopw_base
277{
278 protected:
279 double seconds() const
280 {
281 struct _timeb tb;
282 _ftime(&tb);
283 return (double)tb.time + (double)tb.millitm * 0.001;
284 }
285
286 public:
287 stopwatch_e() : stopw_base(0.001) {}
288};
289
290# else /* _MSC_VER */
291
292class stopwatch_e : public stopw_base
293{
294 protected:
295 double seconds() const
296 {
297 //time_t tm;
298 return (double) time(0);
299 }
300
301 public:
302 stopwatch_e() : stopw_base(1.0) {}
303};
304
305# endif /* _MSC_VER */
306
307#endif /* unix */
308
309#endif /* TBCI_STOPWATCH_H */
#define NULL
Definition basics.h:250
base class for all the stop watches to minimize code duplication
Definition stopwatch.h:87
virtual double seconds() const
Return the current time, to be overriden in derived classes.
Definition stopwatch.h:104
double read_d()
Returns the time since last read/start without stopping the stopwatch (but resets the differential ti...
Definition stopwatch.h:181
virtual ~stopw_base()
d'tor
Definition stopwatch.h:135
double stop()
Stops the stopwatch and returns total time (since last reset).
Definition stopwatch.h:153
int running
The status.
Definition stopwatch.h:101
void start()
Starts the stopwatch.
Definition stopwatch.h:145
stopw_base(const double tick, const double over=-1.0)
c'tor
Definition stopwatch.h:127
double total
The total elapsed time since construction or reset.
Definition stopwatch.h:92
double last_time
The last time we read/started/stopped the clock.
Definition stopwatch.h:90
double stop_d()
Stops the stopwatch and returns time since last start / read.
Definition stopwatch.h:162
double adv_stopwatch()
Advance the stopwatch, return diff, handle single overflows.
Definition stopwatch.h:109
double reset()
Resets the stopwatch.
Definition stopwatch.h:137
const double secs_per_tick
Used in seconds() to convert internal time to s.
Definition stopwatch.h:94
const double overflow_secs
If we overflow once, we miss this many seconds; We assume that internally long ints are used,...
Definition stopwatch.h:99
double read()
Returns the total time without stopping the stopwatch (but resets the differential timer).
Definition stopwatch.h:173
Stopwatch class using gettimeofday() / _ftime() / time() to measure elapsed (= wall clock) time.
Definition stopwatch.h:257
double seconds() const
Return the current time, to be overriden in derived classes.
Definition stopwatch.h:259
Stopwatch class using times() to measure userspace CPU time of ourselves and our children.
Definition stopwatch.h:211
virtual double seconds() const
Return the current time, to be overriden in derived classes.
Definition stopwatch.h:213
Stopwatch class using times() to measure userspace and system CPU time of ourselves and our children.
Definition stopwatch.h:229
virtual double seconds() const
Return the current time, to be overriden in derived classes.
Definition stopwatch.h:231
#define CLK_TCK
This whole CLOCKS_PER_SEC and CLK_TCK business is terribly FUCKED UP !
Definition stopwatch.h:58
#define CPS
Definition stopwatch.h:78
#define LONG_MIN
Definition stopwatch.h:68