2008-05-14 11:57:45 +04:00
|
|
|
#ifndef TIMER_H
|
|
|
|
#define TIMER_H
|
|
|
|
|
|
|
|
#include <ctime>
|
2009-01-07 16:30:06 +03:00
|
|
|
#include <iostream>
|
2008-05-14 11:57:45 +04:00
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
class Timer
|
|
|
|
{
|
2011-11-12 03:58:23 +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".
|
|
|
|
*/
|
2011-02-24 15:42:19 +03:00
|
|
|
friend std::ostream& operator<<(std::ostream& os, Timer& t);
|
2009-01-07 16:30:06 +03:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
private:
|
2008-05-14 11:57:45 +04:00
|
|
|
bool running;
|
|
|
|
time_t start_time;
|
2009-01-07 16:30:06 +03:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* Return the total time that the timer has been in the "running"
|
|
|
|
* state since it was first "started" or last "restarted". For
|
|
|
|
* "short" time periods (less than an hour), the actual cpu time
|
|
|
|
* used is reported instead of the elapsed time.
|
|
|
|
* TODO in seconds?
|
|
|
|
*/
|
2008-05-14 11:57:45 +04:00
|
|
|
double elapsed_time();
|
2009-01-07 16:30:06 +03:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
public:
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* 'running' is initially false. A timer needs to be explicitly started
|
|
|
|
* using 'start' or 'restart'.
|
2009-01-07 16:30:06 +03:00
|
|
|
*/
|
|
|
|
Timer() : running(false), start_time(0) { }
|
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* Start a timer. If it is already running, let it continue running.
|
|
|
|
* Print an optional message.
|
|
|
|
*/
|
2008-05-14 11:57:45 +04:00
|
|
|
void start(const char* msg = 0);
|
2009-01-07 16:30:06 +03:00
|
|
|
// void restart(const char* msg = 0);
|
|
|
|
// void stop(const char* msg = 0);
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Print out an optional message followed by the current timer timing.
|
|
|
|
*/
|
2008-05-14 11:57:45 +04:00
|
|
|
void check(const char* msg = 0);
|
2009-01-07 16:30:06 +03:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* Return the total time that the timer has been in the "running"
|
|
|
|
* state since it was first "started" or last "restarted". For
|
|
|
|
* "short" time periods (less than an hour), the actual cpu time
|
|
|
|
* used is reported instead of the elapsed time.
|
|
|
|
* This function is the public version of elapsed_time()
|
|
|
|
*/
|
|
|
|
double get_elapsed_time();
|
2008-05-14 11:57:45 +04:00
|
|
|
};
|
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
inline std::ostream& operator<<(std::ostream& os, Timer& t)
|
|
|
|
{
|
|
|
|
//os << std::setprecision(2) << std::setiosflags(std::ios::fixed) << (t.running ? t.elapsed_time() : 0);
|
|
|
|
os << (t.running ? t.elapsed_time() : 0);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2011-11-12 02:59:50 +04:00
|
|
|
#endif // TIMER_H
|