00001 #include <iostream>
00002 #include <iomanip>
00003 #include "Util.h"
00004 #include "Timer.h"
00005 #include "StaticData.h"
00006
00007 #include "util/usage.hh"
00008
00009 namespace Moses
00010 {
00011
00012
00013 Timer g_timer;
00014
00015
00016 void ResetUserTime()
00017 {
00018 g_timer.start();
00019 };
00020
00021 void PrintUserTime(const std::string &message)
00022 {
00023 g_timer.check(message.c_str());
00024 }
00025
00026 double GetUserTime()
00027 {
00028 return g_timer.get_elapsed_time();
00029 }
00030
00031
00032
00033
00034
00035
00036 double Timer::get_elapsed_time() const
00037 {
00038 if (stopped) {
00039 return stop_time - start_time;
00040 }
00041 if (running) {
00042 return util::WallTime() - start_time;
00043 }
00044 return 0;
00045 }
00046
00047
00048
00049
00050
00051 void Timer::start(const char* msg)
00052 {
00053
00054 if (msg) VERBOSE(1, msg << std::endl);
00055
00056
00057 if (running && !stopped) return;
00058
00059
00060 if (stopped) {
00061 start_time = util::WallTime() - (stop_time - start_time);
00062 stopped = false;
00063 } else {
00064 start_time = util::WallTime();
00065 running = true;
00066 }
00067 }
00068
00069
00070
00071
00072
00073 void Timer::stop(const char* msg)
00074 {
00075
00076 if (msg) VERBOSE(1, msg << std::endl);
00077
00078
00079 if (stopped || !running) return;
00080
00081
00082 stop_time = util::WallTime();
00083
00084
00085 stopped = true;
00086 }
00087
00088
00089
00090
00091 void Timer::check(const char* msg)
00092 {
00093
00094 if (msg) VERBOSE(1, msg << " : ");
00095
00096
00097 VERBOSE(1, "[" << (running ? get_elapsed_time() : 0) << "] seconds\n");
00098 }
00099
00100
00101
00102
00103
00104
00105 std::ostream& operator<<(std::ostream& os, Timer& t)
00106 {
00107
00108 os << (t.running ? t.get_elapsed_time() : 0);
00109 return os;
00110 }
00111
00112 }
00113