00001 #include <ctime> 00002 #include <iostream> 00003 #include <iomanip> 00004 #include "Util.h" 00005 #include "Timer.h" 00006 00007 namespace Moses 00008 { 00009 00010 /*** 00011 * Return the total time that the timer has been in the "running" 00012 * state since it was first "started" or last "restarted". For 00013 * "short" time periods (less than an hour), the actual cpu time 00014 * used is reported instead of the elapsed time. 00015 */ 00016 double Timer::elapsed_time() 00017 { 00018 time_t now; 00019 time(&now); 00020 return difftime(now, start_time); 00021 } 00022 00023 /*** 00024 * Return the total time that the timer has been in the "running" 00025 * state since it was first "started" or last "restarted". For 00026 * "short" time periods (less than an hour), the actual cpu time 00027 * used is reported instead of the elapsed time. 00028 * This function is the public version of elapsed_time() 00029 */ 00030 double Timer::get_elapsed_time() 00031 { 00032 return elapsed_time(); 00033 } 00034 00035 /*** 00036 * Start a timer. If it is already running, let it continue running. 00037 * Print an optional message. 00038 */ 00039 void Timer::start(const char* msg) 00040 { 00041 // Print an optional message, something like "Starting timer t"; 00042 if (msg) TRACE_ERR( msg << std::endl); 00043 00044 // Return immediately if the timer is already running 00045 if (running) return; 00046 00047 // Change timer status to running 00048 running = true; 00049 00050 // Set the start time; 00051 time(&start_time); 00052 } 00053 00054 /*** 00055 * Turn the timer off and start it again from 0. Print an optional message. 00056 */ 00057 /* 00058 inline void Timer::restart(const char* msg) 00059 { 00060 // Print an optional message, something like "Restarting timer t"; 00061 if (msg) TRACE_ERR( msg << std::endl; 00062 00063 // Set the timer status to running 00064 running = true; 00065 00066 // Set the accumulated time to 0 and the start time to now 00067 acc_time = 0; 00068 start_clock = clock(); 00069 start_time = time(0); 00070 } 00071 */ 00072 00073 /*** 00074 * Stop the timer and print an optional message. 00075 */ 00076 /* 00077 inline void Timer::stop(const char* msg) 00078 { 00079 // Print an optional message, something like "Stopping timer t"; 00080 check(msg); 00081 00082 // Recalculate and store the total accumulated time up until now 00083 if (running) acc_time += elapsed_time(); 00084 00085 running = false; 00086 } 00087 */ 00088 /*** 00089 * Print out an optional message followed by the current timer timing. 00090 */ 00091 void Timer::check(const char* msg) 00092 { 00093 // Print an optional message, something like "Checking timer t"; 00094 if (msg) TRACE_ERR( msg << " : "); 00095 00096 // TRACE_ERR( "[" << std::setiosflags(std::ios::fixed) << std::setprecision(2) << (running ? elapsed_time() : 0) << "] seconds\n"); 00097 TRACE_ERR( "[" << (running ? elapsed_time() : 0) << "] seconds\n"); 00098 } 00099 00100 /*** 00101 * Allow timers to be printed to ostreams using the syntax 'os << t' 00102 * for an ostream 'os' and a timer 't'. For example, "cout << t" will 00103 * print out the total amount of time 't' has been "running". 00104 */ 00105 std::ostream& operator<<(std::ostream& os, Timer& t) 00106 { 00107 //os << std::setprecision(2) << std::setiosflags(std::ios::fixed) << (t.running ? t.elapsed_time() : 0); 00108 os << (t.running ? t.elapsed_time() : 0); 00109 return os; 00110 } 00111 00112 } 00113
1.5.9