Cleanup the Timer class in mert.

This commit is contained in:
Tetsuo Kiso 2012-02-26 14:40:17 +09:00
parent 0dae6e337e
commit a4d8dbcd02
2 changed files with 19 additions and 16 deletions

View File

@ -1,14 +1,14 @@
#include "Timer.h"
#include "Util.h"
double Timer::elapsed_time()
double Timer::elapsed_time() const
{
time_t now;
time(&now);
return difftime(now, m_start_time);
}
double Timer::get_elapsed_time()
double Timer::get_elapsed_time() const
{
return elapsed_time();
}

View File

@ -7,14 +7,7 @@
class Timer
{
/**
* 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".
*/
friend std::ostream& operator<<(std::ostream& os, Timer& t);
private:
private:
bool m_is_running;
time_t m_start_time;
@ -25,14 +18,15 @@ private:
* used is reported instead of the elapsed time.
* TODO in seconds?
*/
double elapsed_time();
double elapsed_time() const;
public:
public:
/**
* 'm_is_running' is initially false. A timer needs to be explicitly started
* using 'start' or 'restart'.
* using 'start'.
*/
Timer() : m_is_running(false), m_start_time(0) { }
~Timer() { }
/**
* Start a timer. If it is already running, let it continue running.
@ -45,6 +39,10 @@ public:
*/
void check(const char* msg = 0);
/**
*/
bool is_running() const { return m_is_running; }
/**
* Return the total time that the timer has been in the "running"
* state since it was first "started" or last "restarted". For
@ -52,12 +50,17 @@ public:
* used is reported instead of the elapsed time.
* This function is the public version of elapsed_time()
*/
double get_elapsed_time();
double get_elapsed_time() const;
};
inline std::ostream& operator<<(std::ostream& os, Timer& t)
/**
* 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".
*/
inline std::ostream& operator<<(std::ostream& os, const Timer& t)
{
os << (t.m_is_running ? t.elapsed_time() : 0);
os << (t.is_running() ? t.get_elapsed_time() : 0);
return os;
}