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 #include <cstdio> 00051 00052 #if defined(_WIN32) && !defined(__CYGWIN__) 00053 #include <windows.h> 00054 00055 int gettimeofday(struct timeval* p, void*) 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 { 00073 00075 Real_Timer __tic_toc_timer; 00076 00077 //---------------------------------------------------------------------------- 00078 // class Timer 00079 //---------------------------------------------------------------------------- 00080 Timer::Timer() 00081 { 00082 reset(); 00083 } 00084 00085 void Timer::start(void) 00086 { 00087 if (!running) { 00088 start_time = get_current_time(); 00089 running = true; 00090 } 00091 } 00092 00093 double Timer::stop(void) 00094 { 00095 if (running) { 00096 stop_time = get_current_time(); 00097 elapsed_time += stop_time - start_time; 00098 running = false; 00099 } 00100 00101 return elapsed_time; 00102 } 00103 00104 void Timer::reset(double t) 00105 { 00106 elapsed_time = t; 00107 start_time = 0; 00108 stop_time = 0; 00109 running = false; 00110 } 00111 00112 double Timer::get_time() const 00113 { 00114 return running ? 00115 elapsed_time + get_current_time() - start_time : 00116 elapsed_time; 00117 } 00118 00119 void Timer::tic(void) 00120 { 00121 reset(); 00122 start(); 00123 } 00124 00125 double Timer::toc(void) 00126 { 00127 return get_time() ; 00128 } 00129 00130 void Timer::toc_print(void) 00131 { 00132 std::cout << "Elapsed time = " << get_time() << " seconds" << std::endl; 00133 } 00134 00135 //---------------------------------------------------------------------------- 00136 // class CPU_Timer 00137 //---------------------------------------------------------------------------- 00138 double CPU_Timer::get_current_time() const 00139 { 00140 return static_cast<double>(clock()) / CLOCKS_PER_SEC; 00141 } 00142 00143 //---------------------------------------------------------------------------- 00144 // class Real_Timer 00145 //---------------------------------------------------------------------------- 00146 double Real_Timer::get_current_time() const 00147 { 00148 struct timeval t; 00149 gettimeofday(&t, 0); 00150 return t.tv_sec + t.tv_usec * 1.0e-6; 00151 } 00152 00153 00154 void tic() 00155 { 00156 __tic_toc_timer.tic(); 00157 } 00158 00159 double toc() 00160 { 00161 return __tic_toc_timer.toc(); 00162 } 00163 00164 void toc_print() 00165 { 00166 __tic_toc_timer.toc_print(); 00167 } 00168 00169 void pause(double t) 00170 { 00171 if (t == -1) { 00172 std::cout << "(Press enter to continue)" << std::endl; 00173 getchar(); 00174 } 00175 else { 00176 Real_Timer T; 00177 T.start(); 00178 while (T.get_time() < t); 00179 } 00180 } 00181 00182 } // namespace itpp
Generated on Sun Jul 26 08:36:48 2009 for IT++ by Doxygen 1.5.9