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