4 #ifndef IBIS_HOROMETER_H
5 #define IBIS_HOROMETER_H
8 #if defined(__sun) || defined(__linux__) || defined(__HOS_AIX__) || \
9 defined(__CYGWIN__) || defined(__APPLE__) || defined(__FreeBSD__)
11 # include <sys/time.h>
12 # include <sys/times.h>
13 # include <sys/resource.h>
15 # define RUSAGE_SELF 0
17 # ifndef RUSAGE_CHILDREN
18 # define RUSAGE_CHILDRED -1
21 # include <sys/times.h>
24 # define RUSAGE_SELF 0
25 # define RUSAGE_CHILDREN -1
26 # include <sys/times.h>
28 # include <sys/time.h>
29 # include <sys/resource.h>
30 #elif defined(__MINGW32__)
32 # include <sys/time.h>
64 horometer() : startRealTime(0), totalRealTime(0),
65 startCPUTime(0), totalCPUTime(0) {
66 #if defined(_WIN32) && defined(_MSC_VER)
68 LARGE_INTEGER lFrequency;
69 BOOL ret = QueryPerformanceFrequency(&lFrequency);
70 if (ret != 0 && lFrequency.QuadPart != 0)
71 countPeriod = 1.0/
static_cast<double>(lFrequency.QuadPart);
78 startRealTime = readWallClock();
79 startCPUTime = readCPUClock();
85 double tmpr = readWallClock() - startRealTime;
86 double tmpc = readCPUClock() - startCPUTime;
88 totalRealTime += tmpr;
94 startRealTime = readWallClock();
95 startCPUTime = readCPUClock();
98 double realTime()
const {
return totalRealTime;}
103 double startRealTime;
104 double totalRealTime;
107 #if defined(_WIN32) && defined(_MSC_VER)
111 inline double readWallClock();
112 inline double readCPUClock();
117 inline double ibis::horometer::readWallClock() {
118 #if defined(CLOCK_MONOTONIC) && !defined(__CYGWIN__)
120 if (0 == clock_gettime(CLOCK_MONOTONIC, &tb)) {
121 return static_cast<double>(tb.tv_sec) + (1e-9 * tb.tv_nsec);
125 gettimeofday(&cpt, 0);
126 return static_cast<double>(cpt.tv_sec) + (1e-6 * cpt.tv_usec);
128 #elif defined(HAVE_GETTIMEOFDAY) || defined(__unix__) || defined(CRAY) || \
129 defined(__linux__) || defined(__HOS_AIX__) || defined(__APPLE__) || \
132 gettimeofday(&cpt, 0);
133 return static_cast<double>(cpt.tv_sec) + (1e-6 * cpt.tv_usec);
134 #elif defined(_WIN32) && defined(_MSC_VER)
136 if (countPeriod != 0) {
138 if (QueryPerformanceCounter(&cnt)) {
139 ret = countPeriod * cnt.QuadPart;
147 GetSystemTimeAsFileTime(&ftRealTime.ftFileTime);
148 ret = (double) ftRealTime.ftInt64 * 1e-7;
152 return (
double) clock() * 0.001;
154 return (
double) clock() / CLOCKS_PER_SEC;
160 inline double ibis::horometer::readCPUClock() {
161 #if defined(__sun) || defined(sgi) || defined(__linux__) || defined(__APPLE__) \
162 || defined(__HOS_AIX__) || defined(__CYGWIN__) || defined(__FreeBSD__)
166 if (0 == getrusage(RUSAGE_SELF, &ruse)) {
167 time = (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec) * 1e-6 +
168 ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec;
171 fputs(
"Warning -- horometer::readCPUClock(): getrusage failed "
172 "on RUSAGE_SELF", stderr);
174 if (0 == getrusage(RUSAGE_CHILDREN, &ruse)) {
175 time += (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec) * 1e-6 +
176 ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec;
179 fputs(
"Warning -- horometer::readCPUClock(): getrusage failed on "
180 "RUSAGE_CHILDRED", stderr);
183 #elif defined(__unix__) || defined(CRAY)
184 #if defined(__STDC__)
187 return (cpt.tms_utime + cpt.tms_stime + cpt.tms_cutime +
188 (
double)cpt.tms_cstime) / CLK_TCK;
190 return (
double) times() / CLK_TCK;
192 #elif defined(_WIN32)
193 return (
double) clock() / CLOCKS_PER_SEC;
195 return (
double) clock() * 0.001;
197 return (
double) clock() / CLOCKS_PER_SEC;
200 #endif // IBIS_HOROMETER_H
void resume()
Continue after being stopped.
Definition: horometer.h:93
void start()
Start the timer. Clear the internal counters.
Definition: horometer.h:77
The current implementation of FastBit is code named IBIS; most data structures and functions are in t...
Definition: bord.h:16
Horometer – a primitive timing instrument.
Definition: horometer.h:62
void stop()
Stop the timer. Record the duration. May resume later.
Definition: horometer.h:84
double CPUTime() const
Return the CPU time in seconds.
Definition: horometer.h:100
double realTime() const
Return the elapsed time in seconds.
Definition: horometer.h:98