30 #include <sys/resource.h>
103 std::string
getTime (
const std::string& AMessage );
108 void display (
const std::string& AMessage );
123 std::string convertTimeval(
const struct timeval& ATimeval )
const;
126 void pickTimeReel(
struct timeval& ATimeval )
const;
129 void pickTimePus(
struct rusage& ARusage )
const;
131 struct rusage FBeforePus;
132 struct rusage FAfterPus;
133 struct rusage FTotalPus;
136 struct timeval FBeforeReel;
137 struct timeval FAfterReel;
138 struct timeval FTotalReel;
151 FDisplayReal(ADisplayReal),
155 FTotalPus.ru_utime.tv_sec = 0; FTotalPus.ru_utime.tv_usec = 0;
157 FTotalPus.ru_stime.tv_sec = 0; FTotalPus.ru_stime.tv_usec = 0;
159 FTotalReel.tv_sec = 0; FTotalReel.tv_usec = 0;
164 FBeforePus (AChrono.FBeforePus),
165 FAfterPus (AChrono.FAfterPus),
166 FTotalPus (AChrono.FTotalPus),
167 FBeforeReel (AChrono.FBeforeReel),
168 FAfterReel (AChrono.FAfterReel),
169 FTotalReel (AChrono.FTotalReel),
170 FDisplayReal(AChrono.FDisplayReal),
171 FMode (AChrono.FMode),
176 void addTimeval(
struct timeval& ATimeval,
const struct timeval& ATimeval2)
178 ATimeval.tv_sec += ATimeval2.tv_sec;
179 ATimeval.tv_usec += ATimeval2.tv_usec;
180 if ( ATimeval.tv_usec >= 10000000 )
183 ATimeval.tv_usec -= 1000000;
190 ATimeval.tv_sec -= ATimeval2.tv_sec;
191 ATimeval.tv_usec -= ATimeval2.tv_usec;
192 if ( ATimeval.tv_usec < 0 )
195 ATimeval.tv_usec += 1000000;
200 void CChrono::pickTimeReel(
struct timeval& ATimeval )
const
202 gettimeofday(&ATimeval,NULL);
206 void CChrono::pickTimePus(
struct rusage& ARusage )
const
208 getrusage(RUSAGE_SELF, &ARusage);
214 FBeforePus = AChrono.FBeforePus;
215 FAfterPus = AChrono.FAfterPus;
216 FTotalPus = AChrono.FTotalPus;
218 FBeforeReel = AChrono.FBeforeReel;
219 FAfterReel = AChrono.FAfterReel;
220 FTotalReel = AChrono.FTotalReel;
222 FDisplayReal = AChrono.FDisplayReal;
223 FMode = AChrono.FMode;
232 addTimeval( FTotalPus.ru_utime, AChrono.FTotalPus.ru_utime );
233 addTimeval( FTotalPus.ru_stime, AChrono.FTotalPus.ru_stime );
272 pickTimePus ( FBeforePus );
273 pickTimeReel( FBeforeReel );
284 pickTimeReel( FAfterReel );
285 pickTimePus ( FAfterPus );
293 addTimeval( FTotalPus.ru_utime, FAfterPus.ru_utime );
294 addTimeval( FTotalPus.ru_stime, FAfterPus.ru_stime );
304 FTotalPus.ru_utime.tv_sec = 0; FTotalPus.ru_utime.tv_usec = 0;
306 FTotalPus.ru_stime.tv_sec = 0; FTotalPus.ru_stime.tv_usec = 0;
308 FTotalReel.tv_sec = 0; FTotalReel.tv_usec = 0;
326 { FDisplayReal = AValue; }
329 std::string CChrono::convertTimeval (
const struct timeval & ATimeval )
const
331 std::ostringstream res;
335 res<<ATimeval.tv_sec<<
'.'
336 <<std::setw(6)<<std::setfill(
'0')<<ATimeval.tv_usec<<
" s";
341 if ( ATimeval.tv_sec!=0 || ATimeval.tv_usec>=150000 )
342 res<<ATimeval.tv_sec<<
'.'
343 <<std::setw(2)<<std::setfill(
'0')
344 <<(int)(ATimeval.tv_usec/10000)<<
" s";
346 if ( ATimeval.tv_usec>1500 )
347 res<<std::setprecision(2)<<std::fixed
348 <<ATimeval.tv_usec/1000.<<
" ms (10-3 s)";
350 res<<ATimeval.tv_usec<<
" us (10-6 s)";
361 std::ostringstream res;
369 res<<AMessage<<
" : ";
372 res<<convertTimeval(FTotalPus.ru_utime)<<
" user, ";
373 res<<convertTimeval(FTotalPus.ru_stime)<<
" sys";
379 res<<convertTimeval(FTotalReel)<<
" reel";
389 { std::cout<<
getTime(AMessage)<<std::endl; }