gravatar
deba@inf.elte.hu
deba@inf.elte.hu
MinGW compatible time measure + changes in its internals
0 1 0
default
1 file changed with 88 insertions and 53 deletions:
↑ Collapse diff ↑
Show white space 6 line context
... ...
@@ -25,8 +25,12 @@
25 25

	
26
#ifdef WIN32
27
#include <windows.h>
28
#include <cmath>
29
#else
26 30
#include <sys/times.h>
31
#include <sys/time.h>
32
#endif
27 33

	
28
#include <sys/time.h>
29 34
#include <fstream>
30 35
#include <iostream>
31
#include <unistd.h>
32 36

	
... ...
@@ -56,21 +60,10 @@
56 60
  {
57
    struct rtms 
58
    {
59
      double tms_utime;
60
      double tms_stime;
61
      double tms_cutime;
62
      double tms_cstime;
63
      rtms() {}
64
      rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
65
		     tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
66
    };
67
    rtms ts;
68
    double real_time;
61
    double utime;
62
    double stime;
63
    double cutime;
64
    double cstime;
65
    double rtime;
69 66
  
70
    rtms &getTms() {return ts;}
71
    const rtms &getTms() const {return ts;}
72

	
73 67
    void _reset() { 
74
      ts.tms_utime = ts.tms_stime = ts.tms_cutime = ts.tms_cstime = 0; 
75
      real_time = 0;
68
      utime = stime = cutime = cstime = rtime = 0;
76 69
    }
... ...
@@ -82,7 +75,36 @@
82 75
    {
76
#ifndef WIN32
83 77
      timeval tv;
84
      tms _ts;
85
      times(&_ts);
86
      gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
87
      ts=_ts;
78
      gettimeofday(&tv, 0);
79
      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
80

	
81
      tms ts;
82
      double tck=sysconf(_SC_CLK_TCK);
83
      times(&ts);
84
      utime=ts.tms_utime/tck;
85
      stime=ts.tms_stime/tck;
86
      cutime=ts.tms_cutime/tck;
87
      cstime=ts.tms_cstime/tck;
88
#else
89
      static const double ch = 4294967296.0e-7;
90
      static const double cl = 1.0e-7;
91

	
92
      FILETIME system;
93
      GetSystemTimeAsFileTime(&system);
94
      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
95

	
96
      FILETIME create, exit, kernel, user;
97
      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
98
	utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
99
	stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
100
	cutime = 0;
101
	cstime = 0;
102
      } else {
103
	rtime = 0;
104
	utime = 0;
105
	stime = 0;
106
	cutime = 0;
107
	cstime = 0;
108
      }
109
#endif      
88 110
    }
... ...
@@ -101,7 +123,7 @@
101 123
    {
102
      ts.tms_utime+=b.ts.tms_utime;
103
      ts.tms_stime+=b.ts.tms_stime;
104
      ts.tms_cutime+=b.ts.tms_cutime;
105
      ts.tms_cstime+=b.ts.tms_cstime;
106
      real_time+=b.real_time;
124
      utime+=b.utime;
125
      stime+=b.stime;
126
      cutime+=b.cutime;
127
      cstime+=b.cstime;
128
      rtime+=b.rtime;
107 129
      return *this;
... ...
@@ -117,7 +139,7 @@
117 139
    {
118
      ts.tms_utime-=b.ts.tms_utime;
119
      ts.tms_stime-=b.ts.tms_stime;
120
      ts.tms_cutime-=b.ts.tms_cutime;
121
      ts.tms_cstime-=b.ts.tms_cstime;
122
      real_time-=b.real_time;
140
      utime-=b.utime;
141
      stime-=b.stime;
142
      cutime-=b.cutime;
143
      cstime-=b.cstime;
144
      rtime-=b.rtime;
123 145
      return *this;
... ...
@@ -133,7 +155,7 @@
133 155
    {
134
      ts.tms_utime*=b;
135
      ts.tms_stime*=b;
136
      ts.tms_cutime*=b;
137
      ts.tms_cstime*=b;
138
      real_time*=b;
156
      utime*=b;
157
      stime*=b;
158
      cutime*=b;
159
      cstime*=b;
160
      rtime*=b;
139 161
      return *this;
... ...
@@ -150,7 +172,7 @@
150 172
    {
151
      ts.tms_utime/=b;
152
      ts.tms_stime/=b;
153
      ts.tms_cutime/=b;
154
      ts.tms_cstime/=b;
155
      real_time/=b;
173
      utime/=b;
174
      stime/=b;
175
      cutime/=b;
176
      cstime/=b;
177
      rtime/=b;
156 178
      return *this;
... ...
@@ -175,3 +197,3 @@
175 197
    {
176
      return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
198
      return utime;
177 199
    }
... ...
@@ -180,16 +202,22 @@
180 202
    {
181
      return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
203
      return stime;
182 204
    }
183 205
    ///Gives back the user time of the process' children
206

	
207
    ///\note On <tt>WIN32</tt> platform this value is not calculated. 
208
    ///
184 209
    double cUserTime() const
185 210
    {
186
      return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
211
      return cutime;
187 212
    }
188 213
    ///Gives back the user time of the process' children
214

	
215
    ///\note On <tt>WIN32</tt> platform this value is not calculated. 
216
    ///
189 217
    double cSystemTime() const
190 218
    {
191
      return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
219
      return cstime;
192 220
    }
193 221
    ///Gives back the real time
194
    double realTime() const {return real_time;}
222
    double realTime() const {return rtime;}
195 223
  };
... ...
@@ -214,9 +242,10 @@
214 242
  /// \relates TimeStamp
243
  /// \note On <tt>WIN32</tt> platform the cummulative values are not
244
  /// calculated.
215 245
  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
216 246
  {
217
    long cls = sysconf(_SC_CLK_TCK);
218
    os << "u: " << double(t.getTms().tms_utime)/cls <<
219
      "s, s: " << double(t.getTms().tms_stime)/cls <<
220
      "s, cu: " << double(t.getTms().tms_cutime)/cls <<
221
      "s, cs: " << double(t.getTms().tms_cstime)/cls <<
247
    os << "u: " << t.userTime() <<
248
      "s, s: " << t.systemTime() <<
249
      "s, cu: " << t.cUserTime() <<
250
      "s, cs: " << t.cSystemTime() <<
222 251
      "s, real: " << t.realTime() << "s";
... ...
@@ -406,2 +435,5 @@
406 435
    ///Gives back the ellapsed user time of the process' children
436

	
437
    ///\note On <tt>WIN32</tt> platform this value is not calculated. 
438
    ///
407 439
    double cUserTime() const
... ...
@@ -411,2 +443,5 @@
411 443
    ///Gives back the ellapsed user time of the process' children
444

	
445
    ///\note On <tt>WIN32</tt> platform this value is not calculated. 
446
    ///
412 447
    double cSystemTime() const
0 comments (0 inline)