00001 #ifndef MERT_TIMER_H_
00002 #define MERT_TIMER_H_
00003
00004 #include <ostream>
00005 #include <string>
00006 #include <stdint.h>
00007
00008 namespace MosesTuning
00009 {
00010
00011
00012 class Timer
00013 {
00014 private:
00015
00016 struct CPUTime {
00017 uint64_t user_time;
00018 uint64_t sys_time;
00019
00020 CPUTime() : user_time(0), sys_time(0) { }
00021 };
00022
00023 void GetCPUTimeMicroSeconds(CPUTime* cpu_time) const;
00024
00025 bool m_is_running;
00026 uint64_t m_wall;
00027 CPUTime m_start_time;
00028
00029
00030 Timer(const Timer&);
00031 void operator=(const Timer&);
00032
00033 public:
00038 Timer()
00039 : m_is_running(false),
00040 m_wall(0),
00041 m_start_time() {}
00042
00043 ~Timer() {}
00044
00049 void start(const char* msg = 0);
00050
00055 void restart(const char* msg = 0);
00056
00060 void check(const char* msg = 0);
00061
00064 bool is_running() const {
00065 return m_is_running;
00066 }
00067
00074 double get_elapsed_cpu_time() const;
00075
00079 uint64_t get_elapsed_cpu_time_microseconds() const;
00080
00084 double get_elapsed_wall_time() const;
00085
00089 uint64_t get_elapsed_wall_time_microseconds() const;
00090
00094 std::string ToString() const;
00095 };
00096
00102 inline std::ostream& operator<<(std::ostream& os, const Timer& t)
00103 {
00104 if (t.is_running()) {
00105 os << t.ToString();
00106 } else {
00107 os << "timer is not running.";
00108 }
00109 return os;
00110 }
00111
00112 }
00113
00114
00115 #endif // MERT_TIMER_H_