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