00001 00030 #ifndef _MSC_VER 00031 # include <itpp/config.h> 00032 #else 00033 # include <itpp/config_msvc.h> 00034 #endif 00035 00036 #ifdef TIME_WITH_SYS_TIME 00037 # include <sys/time.h> 00038 # include <ctime> 00039 #else 00040 # ifdef HAVE_SYS_TIME_H 00041 # include <sys/time.h> 00042 # else 00043 # include <ctime> 00044 # endif 00045 #endif 00046 00047 #include <itpp/base/timing.h> 00048 #include <iostream> 00049 #include <cmath> 00050 00051 00052 #if defined(_WIN32) && !defined(__CYGWIN__) 00053 #include <windows.h> 00054 00055 int gettimeofday(struct timeval* p, void* tz) 00056 { 00057 union { 00058 long long ns100; /* time since 1 Jan 1601 in 100ns units */ 00059 FILETIME ft; 00060 } _now; 00061 00062 GetSystemTimeAsFileTime(&(_now.ft)); 00063 p->tv_usec = (long)((_now.ns100 / 10LL) % 1000000LL); 00064 /* time since 1 Jan 1970 */ 00065 p->tv_sec= (long)((_now.ns100 - 116444736000000000LL) / 10000000LL); 00066 return 0; 00067 } 00068 #endif 00069 00070 00071 namespace itpp { 00072 00074 Real_Timer __tic_toc_timer; 00075 00076 //---------------------------------------------------------------------------- 00077 // class Timer 00078 //---------------------------------------------------------------------------- 00079 Timer::Timer() 00080 { 00081 reset(); 00082 } 00083 00084 void Timer::start(void) 00085 { 00086 if (!running) { 00087 start_time = get_current_time(); 00088 running = true; 00089 } 00090 } 00091 00092 double Timer::stop(void) 00093 { 00094 if (running) { 00095 stop_time = get_current_time(); 00096 elapsed_time += stop_time-start_time; 00097 running = false; 00098 } 00099 00100 return elapsed_time; 00101 } 00102 00103 void Timer::reset(double t) 00104 { 00105 elapsed_time = t; 00106 start_time = 0; 00107 stop_time = 0; 00108 running = false; 00109 } 00110 00111 double Timer::get_time() const 00112 { 00113 return running ? 00114 elapsed_time + get_current_time() - start_time : 00115 elapsed_time; 00116 } 00117 00118 void Timer::tic(void) 00119 { 00120 reset(); 00121 start(); 00122 } 00123 00124 double Timer::toc(void) 00125 { 00126 return get_time() ; 00127 } 00128 00129 void Timer::toc_print(void) 00130 { 00131 std::cout << "Elapsed time = " << get_time() << " seconds" << std::endl; 00132 } 00133 00134 //---------------------------------------------------------------------------- 00135 // class CPU_Timer 00136 //---------------------------------------------------------------------------- 00137 double CPU_Timer::get_current_time() const 00138 { 00139 return static_cast<double>(clock()) / CLOCKS_PER_SEC; 00140 } 00141 00142 //---------------------------------------------------------------------------- 00143 // class Real_Timer 00144 //---------------------------------------------------------------------------- 00145 double Real_Timer::get_current_time() const 00146 { 00147 struct timeval t; 00148 gettimeofday(&t, 0); 00149 return t.tv_sec + t.tv_usec * 1.0e-6; 00150 } 00151 00152 00153 void tic() 00154 { 00155 __tic_toc_timer.tic(); 00156 } 00157 00158 double toc() 00159 { 00160 return __tic_toc_timer.toc(); 00161 } 00162 00163 void toc_print() 00164 { 00165 __tic_toc_timer.toc_print(); 00166 } 00167 00168 void pause(double t) 00169 { 00170 if (t==-1) { 00171 std::cout << "(Press enter to continue)" << std::endl; 00172 getchar(); 00173 } else { 00174 Real_Timer T; 00175 T.start(); 00176 while (T.get_time()<t); 00177 } 00178 } 00179 00180 } // namespace itpp
Generated on Thu Apr 24 13:38:59 2008 for IT++ by Doxygen 1.5.5