1 | // ===================================================================
|
---|
2 | // $Id: timer.cpp,v $
|
---|
3 | //
|
---|
4 | // timer.cpp
|
---|
5 | // Timing for running process
|
---|
6 | //
|
---|
7 | // REPLACEMENT_STRING
|
---|
8 | //
|
---|
9 | // Copyright by Vlastimil Havran, 2007 - email to "vhavran AT seznam.cz"
|
---|
10 | // Windows time measuring added by Jaroslav Krivanek, October 2003.
|
---|
11 |
|
---|
12 | // GOLEM headers
|
---|
13 | #include "timer.h"
|
---|
14 |
|
---|
15 | // ------------------------------------------------------
|
---|
16 | // Use ftime instead of time, see > man ftime
|
---|
17 | #define _USE_FTIME_CALL
|
---|
18 |
|
---|
19 | // standard headers
|
---|
20 | #include <ctime>
|
---|
21 | #ifdef __UNIX__
|
---|
22 |
|
---|
23 | #include <sys/time.h>
|
---|
24 | #include <sys/resource.h>
|
---|
25 |
|
---|
26 | #ifdef _USE_FTIME_CALL
|
---|
27 | #include <sys/timeb.h>
|
---|
28 | #endif // _USE_FTIME_CALL
|
---|
29 |
|
---|
30 | #endif // __UNIX__
|
---|
31 | #ifdef _MSC_VER
|
---|
32 | #include <windows.h>
|
---|
33 | #include <sys/types.h>
|
---|
34 | #include <sys/timeb.h>
|
---|
35 | #endif // _MSC_VER
|
---|
36 |
|
---|
37 | namespace GtpVisibilityPreprocessor {
|
---|
38 |
|
---|
39 |
|
---|
40 | // Staic variable
|
---|
41 | bool
|
---|
42 | CTimer::initTimingCalled = false;
|
---|
43 |
|
---|
44 | // The following are not static's in the CTimer class to prevent
|
---|
45 | // #including <windows.h> into the "timer.h" header.
|
---|
46 | #ifdef _MSC_VER
|
---|
47 | /// true if the performance timer works under windows
|
---|
48 | static BOOL hasHRTimer;
|
---|
49 | /// frequency of the performance timer under windows
|
---|
50 | static LARGE_INTEGER hrFreq;
|
---|
51 | #endif // MSC_VER
|
---|
52 |
|
---|
53 | void
|
---|
54 | CTimer::_initTiming()
|
---|
55 | {
|
---|
56 | #ifdef _MSC_VER
|
---|
57 | hasHRTimer = QueryPerformanceFrequency(&hrFreq);
|
---|
58 | #endif
|
---|
59 | initTimingCalled = true;
|
---|
60 | }
|
---|
61 |
|
---|
62 | void
|
---|
63 | CTimer::Reset()
|
---|
64 | {
|
---|
65 | // Real times in seconds
|
---|
66 | #ifdef _USE_FTIME_CALL
|
---|
67 | timeb beg;
|
---|
68 | ftime(&beg);
|
---|
69 | lastRealTime = (double)(beg.time + 0.001 * beg.millitm);
|
---|
70 | #else // _USE_FTIME_CALL
|
---|
71 | time_t beg;
|
---|
72 | time(&beg);
|
---|
73 | lastRealTime = (double)beg;
|
---|
74 | #endif // _USE_FTIME_CALL
|
---|
75 |
|
---|
76 | lastUserTime = 0.0;
|
---|
77 | lastSystemTime = 0.0;
|
---|
78 |
|
---|
79 | realTime = 0.0;
|
---|
80 | userTime = 0.0;
|
---|
81 | systemTime = 0.0;
|
---|
82 |
|
---|
83 | countStop = 0;
|
---|
84 | running = false;
|
---|
85 |
|
---|
86 | #ifdef __UNIX__
|
---|
87 | // Timing in OS UNIX
|
---|
88 | begrusage.ru_utime.tv_usec = begrusage.ru_utime.tv_sec =
|
---|
89 | begrusage.ru_stime.tv_usec = begrusage.ru_stime.tv_sec = 0L;
|
---|
90 | endrusage = begrusage;
|
---|
91 | #endif // __UNIX__
|
---|
92 | }
|
---|
93 |
|
---|
94 | void
|
---|
95 | CTimer::_start() const
|
---|
96 | {
|
---|
97 | if (running)
|
---|
98 | return; // timer is already running
|
---|
99 |
|
---|
100 | // Measure the real time
|
---|
101 | #ifdef _USE_FTIME_CALL
|
---|
102 | timeb beg;
|
---|
103 | ftime(&beg);
|
---|
104 | lastRealTime = (double)(beg.time + 0.001 * beg.millitm);
|
---|
105 | #else // _USE_FTIME_CALL
|
---|
106 | time_t beg;
|
---|
107 | time(&beg);
|
---|
108 | lastRealTime = (double)beg;
|
---|
109 | #endif // _USE_FTIME_CALL
|
---|
110 |
|
---|
111 | // Measure the real and system time
|
---|
112 | #ifdef __UNIX__
|
---|
113 | // Measure under UNIX
|
---|
114 | struct rusage begrusage;
|
---|
115 | getrusage(RUSAGE_SELF, &begrusage);
|
---|
116 |
|
---|
117 | lastUserTime = (double)begrusage.ru_utime.tv_sec +
|
---|
118 | 1e-6 * begrusage.ru_utime.tv_usec;
|
---|
119 |
|
---|
120 | lastSystemTime = (double)begrusage.ru_stime.tv_sec +
|
---|
121 | 1e-6 * begrusage.ru_stime.tv_usec;
|
---|
122 | #endif // __UNIX__
|
---|
123 |
|
---|
124 | #ifdef _MSC_VER
|
---|
125 | // Mesure under Windows
|
---|
126 | if (hasHRTimer) {
|
---|
127 | LARGE_INTEGER counter;
|
---|
128 | QueryPerformanceCounter(&counter);
|
---|
129 | lastUserTime = (double)counter.QuadPart / (double)hrFreq.QuadPart;
|
---|
130 | lastSystemTime = 0;
|
---|
131 | }
|
---|
132 | else {
|
---|
133 | static struct _timeb mtime;
|
---|
134 | _ftime(&mtime);
|
---|
135 | lastUserTime = (double)mtime.time + 1e-3 * mtime.millitm;
|
---|
136 | lastSystemTime = 0;
|
---|
137 | }
|
---|
138 | #endif
|
---|
139 | // Begin trace.
|
---|
140 | running = true;
|
---|
141 | }
|
---|
142 |
|
---|
143 | void
|
---|
144 | CTimer::_stop() const
|
---|
145 | {
|
---|
146 | if (!running)
|
---|
147 | return; // timer is not running
|
---|
148 |
|
---|
149 | // Begin trace.
|
---|
150 | #ifdef _USE_FTIME_CALL
|
---|
151 | timeb end;
|
---|
152 | ftime(&end);
|
---|
153 | realTime += (double)(end.time + 0.001 * end.millitm) - lastRealTime;
|
---|
154 | #else // _USE_FTIME_CALL
|
---|
155 | time_t end;
|
---|
156 | time(&end);
|
---|
157 | realTime += (double)end - lastRealTime;
|
---|
158 | #endif // _USE_FTIME_CALL
|
---|
159 |
|
---|
160 | #ifdef __UNIX__
|
---|
161 | // timing in unix OS
|
---|
162 | getrusage(RUSAGE_SELF, &endrusage);
|
---|
163 |
|
---|
164 | userTime += (double)endrusage.ru_utime.tv_sec +
|
---|
165 | 1e-6 * endrusage.ru_utime.tv_usec -
|
---|
166 | lastUserTime;
|
---|
167 |
|
---|
168 | systemTime += (double)endrusage.ru_stime.tv_sec +
|
---|
169 | 1e-6 * endrusage.ru_stime.tv_usec -
|
---|
170 | lastSystemTime;
|
---|
171 | #endif // __UNIX__
|
---|
172 |
|
---|
173 | #ifdef _MSC_VER
|
---|
174 | // Mesure under Windows
|
---|
175 | if (hasHRTimer) {
|
---|
176 | LARGE_INTEGER counter;
|
---|
177 | QueryPerformanceCounter(&counter);
|
---|
178 | userTime += (double)counter.QuadPart / (double)hrFreq.QuadPart -
|
---|
179 | lastUserTime;
|
---|
180 | systemTime = 0;
|
---|
181 | }
|
---|
182 | else {
|
---|
183 | static struct _timeb mtime;
|
---|
184 | _ftime(&mtime);
|
---|
185 | userTime += (double)mtime.time + 1e-3 * mtime.millitm -
|
---|
186 | lastUserTime;
|
---|
187 | systemTime = 0;
|
---|
188 | }
|
---|
189 | #endif
|
---|
190 |
|
---|
191 | running = false;
|
---|
192 | countStop++;
|
---|
193 | }
|
---|
194 |
|
---|
195 | // returns the real time measured by timer in seconds
|
---|
196 | double
|
---|
197 | CTimer::RealTime() const
|
---|
198 | {
|
---|
199 | if (running) {
|
---|
200 | _stop();
|
---|
201 | _start();
|
---|
202 | }
|
---|
203 | return realTime;
|
---|
204 | }
|
---|
205 |
|
---|
206 | // returns the user time measured by timer in seconds
|
---|
207 | double
|
---|
208 | CTimer::UserTime() const
|
---|
209 | {
|
---|
210 | if (running) {
|
---|
211 | _stop();
|
---|
212 | _start();
|
---|
213 | }
|
---|
214 | return userTime;
|
---|
215 | }
|
---|
216 |
|
---|
217 | // returns the user+system time measured by timer in seconds
|
---|
218 | double
|
---|
219 | CTimer::SystemTime() const
|
---|
220 | {
|
---|
221 | if (running) {
|
---|
222 | _stop();
|
---|
223 | _start();
|
---|
224 | }
|
---|
225 | return systemTime;
|
---|
226 | }
|
---|
227 |
|
---|
228 | }
|
---|