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