2008-09-12 19:31:46 +04:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include "Util.h"
|
|
|
|
#include "Timer.h"
|
|
|
|
|
2014-01-02 01:19:06 +04:00
|
|
|
#include "util/usage.hh"
|
2008-10-09 03:51:26 +04:00
|
|
|
|
2014-01-02 01:19:06 +04:00
|
|
|
namespace Moses
|
2008-09-12 19:31:46 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
/***
|
2014-01-02 01:19:06 +04:00
|
|
|
* Return the total wall time that the timer has been in the "running"
|
|
|
|
* state since it was first "started".
|
2008-09-12 19:31:46 +04:00
|
|
|
*/
|
2014-01-03 22:45:31 +04:00
|
|
|
double Timer::get_elapsed_time() const
|
2008-09-12 19:31:46 +04:00
|
|
|
{
|
2014-01-03 22:45:31 +04:00
|
|
|
if (stopped) {
|
|
|
|
return stop_time - start_time;
|
|
|
|
}
|
|
|
|
if (running) {
|
|
|
|
return util::WallTime() - start_time;
|
|
|
|
}
|
|
|
|
return 0;
|
2008-09-12 19:31:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Start a timer. If it is already running, let it continue running.
|
|
|
|
* Print an optional message.
|
|
|
|
*/
|
|
|
|
void Timer::start(const char* msg)
|
|
|
|
{
|
|
|
|
// Print an optional message, something like "Starting timer t";
|
2011-02-24 16:14:42 +03:00
|
|
|
if (msg) TRACE_ERR( msg << std::endl);
|
2008-09-12 19:31:46 +04:00
|
|
|
|
|
|
|
// Return immediately if the timer is already running
|
2014-01-03 22:45:31 +04:00
|
|
|
if (running && !stopped) return;
|
2008-09-12 19:31:46 +04:00
|
|
|
|
2014-01-03 22:45:31 +04:00
|
|
|
// If stopped, recompute start time
|
|
|
|
if (stopped) {
|
|
|
|
start_time = util::WallTime() - (stop_time - start_time);
|
|
|
|
stopped = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
start_time = util::WallTime();
|
|
|
|
running = true;
|
|
|
|
}
|
|
|
|
}
|
2008-09-12 19:31:46 +04:00
|
|
|
|
2014-01-03 22:45:31 +04:00
|
|
|
/***
|
|
|
|
* Stop a timer.
|
|
|
|
* Print an optional message.
|
|
|
|
*/
|
|
|
|
void Timer::stop(const char* msg)
|
|
|
|
{
|
|
|
|
// Print an optional message, something like "Stopping timer t";
|
|
|
|
if (msg) TRACE_ERR( msg << std::endl);
|
|
|
|
|
|
|
|
// Return immediately if the timer is not running
|
|
|
|
if (stopped || !running) return;
|
|
|
|
|
|
|
|
// Record stopped time
|
|
|
|
stop_time = util::WallTime();
|
|
|
|
|
|
|
|
// Change timer status to running
|
|
|
|
stopped = true;
|
2008-09-12 19:31:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Print out an optional message followed by the current timer timing.
|
|
|
|
*/
|
|
|
|
void Timer::check(const char* msg)
|
|
|
|
{
|
|
|
|
// Print an optional message, something like "Checking timer t";
|
|
|
|
if (msg) TRACE_ERR( msg << " : ");
|
|
|
|
|
|
|
|
// TRACE_ERR( "[" << std::setiosflags(std::ios::fixed) << std::setprecision(2) << (running ? elapsed_time() : 0) << "] seconds\n");
|
2014-01-02 01:19:06 +04:00
|
|
|
TRACE_ERR( "[" << (running ? get_elapsed_time() : 0) << "] seconds\n");
|
2008-09-12 19:31:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Allow timers to be printed to ostreams using the syntax 'os << t'
|
|
|
|
* for an ostream 'os' and a timer 't'. For example, "cout << t" will
|
|
|
|
* print out the total amount of time 't' has been "running".
|
|
|
|
*/
|
|
|
|
std::ostream& operator<<(std::ostream& os, Timer& t)
|
|
|
|
{
|
|
|
|
//os << std::setprecision(2) << std::setiosflags(std::ios::fixed) << (t.running ? t.elapsed_time() : 0);
|
2014-01-02 01:19:06 +04:00
|
|
|
os << (t.running ? t.get_elapsed_time() : 0);
|
2008-09-12 19:31:46 +04:00
|
|
|
return os;
|
|
|
|
}
|
2008-10-09 03:51:26 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|