TimerStat: better precision for wallclock time.

This commit is contained in:
Benjamin BOUVIER 2012-09-18 22:03:07 -04:00
commit 9d4742c995

View file

@ -22,7 +22,7 @@ Authors:
# ifndef __EO_TIMER_H__ # ifndef __EO_TIMER_H__
# define __EO_TIMER_H__ # define __EO_TIMER_H__
# include <sys/time.h> // time() # include <sys/time.h> // gettimeofday()
# include <sys/resource.h> // rusage() # include <sys/resource.h> // rusage()
# include <vector> // std::vector # include <vector> // std::vector
@ -61,7 +61,7 @@ class eoTimer
*/ */
void restart() void restart()
{ {
wc_start = time(NULL); gettimeofday( &wc_start, NULL );
getrusage( RUSAGE_SELF, &_start ); getrusage( RUSAGE_SELF, &_start );
} }
@ -138,7 +138,9 @@ class eoTimer
*/ */
double wallclock() double wallclock()
{ {
return std::difftime( std::time(NULL) , wc_start ); struct timeval wc_end;
gettimeofday( &wc_end, NULL );
return ( wc_end.tv_sec - wc_start.tv_sec ) + ( wc_end.tv_usec - wc_start.tv_usec ) / 1000000.;
} }
protected: protected:
@ -149,7 +151,7 @@ class eoTimer
// Remainder (in milliseconds) for system time. // Remainder (in milliseconds) for system time.
long int usremainder; long int usremainder;
// Structure used to measure wallclock time. // Structure used to measure wallclock time.
time_t wc_start; struct timeval wc_start;
}; };
/** /**