IT++ Logo Newcom Logo

timing.cpp

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

Generated on Fri Jan 11 08:51:37 2008 for IT++ by Doxygen 1.3.9.1