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 { return m_is_running; }
00065
00072 double get_elapsed_cpu_time() const;
00073
00077 uint64_t get_elapsed_cpu_time_microseconds() const;
00078
00082 double get_elapsed_wall_time() const;
00083
00087 uint64_t get_elapsed_wall_time_microseconds() const;
00088
00092 std::string ToString() const;
00093 };
00094
00100 inline std::ostream& operator<<(std::ostream& os, const Timer& t) {
00101 if (t.is_running()) {
00102 os << t.ToString();
00103 } else {
00104 os << "timer is not running.";
00105 }
00106 return os;
00107 }
00108
00109 }
00110
00111
00112 #endif // MERT_TIMER_H_