mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-09-11 11:25:40 +03:00
Make time more precise on platforms that have CLOCK_MONOTONIC (otherwise, we'll only have second-level precision)
This commit is contained in:
parent
533b0852f8
commit
687f407d62
@ -15,9 +15,17 @@ namespace Moses
|
||||
*/
|
||||
double Timer::elapsed_time()
|
||||
{
|
||||
#ifdef CLOCK_MONOTONIC
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
double elapsed = (now.tv_sec - start_time.tv_sec);
|
||||
elapsed += (now.tv_nsec - start_time.tv_nsec) / 1000000000.0;
|
||||
return elapsed;
|
||||
#else
|
||||
time_t now;
|
||||
time(&now);
|
||||
return difftime(now, start_time);
|
||||
#endif
|
||||
}
|
||||
|
||||
/***
|
||||
@ -48,7 +56,11 @@ void Timer::start(const char* msg)
|
||||
running = true;
|
||||
|
||||
// Set the start time;
|
||||
#ifdef CLOCK_MONOTONIC
|
||||
clock_gettime(CLOCK_MONOTONIC, &start_time);
|
||||
#else
|
||||
time(&start_time);
|
||||
#endif
|
||||
}
|
||||
|
||||
/***
|
||||
|
@ -10,6 +10,9 @@ namespace Moses
|
||||
{
|
||||
|
||||
/** Wrapper around time_t to time how long things have been running
|
||||
* according to walltime. We avoid CPU time since it is less reliable
|
||||
* in a multi-threaded environment and can spuriously include clock cycles
|
||||
* used by other threads in the same process.
|
||||
*/
|
||||
class Timer
|
||||
{
|
||||
@ -17,7 +20,13 @@ class Timer
|
||||
|
||||
private:
|
||||
bool running;
|
||||
// note: this only has the resolution of seconds, we'd often like better resolution
|
||||
// we make our best effort to do this on a system-by-system basis
|
||||
#ifdef CLOCK_MONOTONIC
|
||||
struct timespec start_time;
|
||||
#else
|
||||
time_t start_time;
|
||||
#endif
|
||||
|
||||
// in seconds
|
||||
double elapsed_time();
|
||||
@ -27,7 +36,14 @@ public:
|
||||
* 'running' is initially false. A timer needs to be explicitly started
|
||||
* using 'start' or 'restart'
|
||||
*/
|
||||
Timer() : running(false), start_time(0) { }
|
||||
Timer() : running(false) {
|
||||
#ifdef CLOCK_MONOTONIC
|
||||
start_time.tv_sec = 0;
|
||||
start_time.tv_nsec = 0;
|
||||
#else
|
||||
start_time = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void start(const char* msg = 0);
|
||||
// void restart(const char* msg = 0);
|
||||
|
Loading…
Reference in New Issue
Block a user