Changeset 1689:f1795dafe42c in lemon0.x for lemon/time_measure.h
 Timestamp:
 09/28/05 10:14:39 (14 years ago)
 Branch:
 default
 Phase:
 public
 Convert:
 svn:c9d7d8f590d60310b91f818b3a526b0e/lemon/trunk@2213
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

lemon/time_measure.h
r1435 r1689 51 51 class TimeStamp 52 52 { 53 tms ts; 53 struct rtms 54 { 55 double tms_utime; 56 double tms_stime; 57 double tms_cutime; 58 double tms_cstime; 59 rtms() {} 60 rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime), 61 tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {} 62 }; 63 rtms ts; 54 64 double real_time; 55 65 66 rtms &getTms() {return ts;} 67 const rtms &getTms() const {return ts;} 68 56 69 public: 57 70 58 tms &getTms() {return ts;}59 const tms &getTms() const {return ts;}60 71 ///Read the current time values of the process 61 72 void stamp() 62 73 { 63 74 timeval tv; 64 times(&ts); 75 tms _ts; 76 times(&_ts); 65 77 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6; 78 ts=_ts; 66 79 } 67 80 … … 104 117 return t=b; 105 118 } 119 ///\e 120 121 ///\bug operator * and / gives rounded values! 122 TimeStamp &operator*=(double b) 123 { 124 ts.tms_utime*=b; 125 ts.tms_stime*=b; 126 ts.tms_cutime*=b; 127 ts.tms_cstime*=b; 128 real_time*=b; 129 return *this; 130 } 131 ///\e 132 TimeStamp operator*(double b) const 133 { 134 TimeStamp t(*this); 135 return t*=b; 136 } 137 friend TimeStamp operator*(double b,const TimeStamp &t); 138 ///\e 139 TimeStamp &operator/=(double b) 140 { 141 ts.tms_utime/=b; 142 ts.tms_stime/=b; 143 ts.tms_cutime/=b; 144 ts.tms_cstime/=b; 145 real_time/=b; 146 return *this; 147 } 148 ///\e 149 TimeStamp operator/(double b) const 150 { 151 TimeStamp t(*this); 152 return t/=b; 153 } 106 154 ///The time ellapsed since the last call of stamp() 107 155 TimeStamp ellapsed() const … … 114 162 115 163 ///Gives back the user time of the process 116 double getUserTime() const164 double userTime() const 117 165 { 118 166 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK); 119 167 } 120 168 ///Gives back the system time of the process 121 double getSystemTime() const169 double systemTime() const 122 170 { 123 171 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK); 124 172 } 125 173 ///Gives back the user time of the process' children 126 double getCUserTime() const174 double cUserTime() const 127 175 { 128 176 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK); 129 177 } 130 178 ///Gives back the user time of the process' children 131 double getCSystemTime() const179 double cSystemTime() const 132 180 { 133 181 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK); 134 182 } 135 183 ///Gives back the real time of the process 136 double getRealTime() const {return real_time;}184 double realTime() const {return real_time;} 137 185 }; 138 186 187 TimeStamp operator*(double b,const TimeStamp &t) 188 { 189 return t*b; 190 } 191 139 192 ///Class measuring the cpu time and real time usage of the process 140 193 … … 198 251 199 252 ///Gives back the ellapsed user time of the process 200 double getUserTime() const201 { 202 return operator TimeStamp(). getUserTime();253 double userTime() const 254 { 255 return operator TimeStamp().userTime(); 203 256 } 204 257 ///Gives back the ellapsed system time of the process 205 double getSystemTime() const206 { 207 return operator TimeStamp(). getSystemTime();258 double systemTime() const 259 { 260 return operator TimeStamp().systemTime(); 208 261 } 209 262 ///Gives back the ellapsed user time of the process' children 210 double getCUserTime() const211 { 212 return operator TimeStamp(). getCUserTime();263 double cUserTime() const 264 { 265 return operator TimeStamp().cUserTime(); 213 266 } 214 267 ///Gives back the ellapsed user time of the process' children 215 double getCSystemTime() const216 { 217 return operator TimeStamp(). getCSystemTime();268 double cSystemTime() const 269 { 270 return operator TimeStamp().cSystemTime(); 218 271 } 219 272 ///Gives back the ellapsed real time of the process 220 double getRealTime() const221 { 222 return operator TimeStamp(). getRealTime();273 double realTime() const 274 { 275 return operator TimeStamp().realTime(); 223 276 } 224 277 … … 245 298 "s, cu: " << double(t.getTms().tms_cutime)/cls << 246 299 "s, cs: " << double(t.getTms().tms_cstime)/cls << 247 "s, real: " << t. getRealTime() << "s";300 "s, real: " << t.realTime() << "s"; 248 301 return os; 249 302 } 250 303 304 305 ///Tool to measure the running time more exactly. 306 307 ///This function calls \c f several times and returns the average 308 ///running time. The number of the executions will be choosen in such a way 309 ///that the full running time will be roughly between \c min_time 310 ///and <tt>2*min_time</tt>. 311 ///\param f the function object to be measured. 312 ///\param min_time the minimum total running time. 313 ///\retval num if it is not \c NULL, then *num will contain the actual 314 /// number of execution of \c f. 315 ///\retval full_time if it is not \c NULL, then *full_time 316 /// will contain the actual 317 /// total running time. 318 ///\return The average running time of \c f. 319 320 template<class F> 321 TimeStamp runningTimeTest(F &f,double min_time=10,int *num = NULL, 322 TimeStamp *full_time=NULL) 323 { 324 Timer t; 325 TimeStamp full; 326 int total=0; 327 for(int tn=1;tn < 1<<24; tn*=2) { 328 for(;total<tn;total++) f(); 329 full=t; 330 if(full.realTime()>min_time) { 331 if(num) *num=total; 332 if(full_time) *full_time=full; 333 return full/total; 334 } 335 } 336 return TimeStamp(); 337 } 338 251 339 /// @} 252 340 341 253 342 } //namespace lemon 254 343
Note: See TracChangeset
for help on using the changeset viewer.