CPTimer.h
Go to the documentation of this file.
00001 
00005 // Cross Platform Timer
00006 // Adapted from Boost Library
00007 
00008 #ifndef CPTIMER_H
00009 #define CPTIMER_H
00010 
00011 
00012 
00013 #ifdef _MSC_VER
00014 
00015 #include <ctime>
00016 
00017 #else
00018 
00019 #include <stdlib.h>
00020 
00021 //for timing under Linux
00022 #include <sys/param.h>
00023 #include <sys/types.h>
00024 #include <sys/times.h>
00025 #include <unistd.h>
00026 
00027 #endif
00028 
00029 class CPTimer
00030 {
00031 #ifdef _MSC_VER 
00032         // Windows visual studio version
00033 public:
00034         CPTimer()
00035         { 
00036                 _start_time = std::clock(); 
00037                 _elapsed = 0.0;
00038                 paused = false;
00039 
00040         }
00041         // postcondition: elapsed()==0
00042         
00043         void   start()
00044         { 
00045                 _start_time = std::clock(); 
00046                 _elapsed = 0.0;
00047                 paused = false;
00048 
00049         } // post: elapsed()==0
00050         
00051         void   restart()
00052         {
00053                  _start_time = std::clock();
00054                  _elapsed = 0.0;
00055                  paused = false;
00056 
00057         } // post: elapsed()==0
00058         
00059         void resume()
00060         {
00061                 if(paused)
00062                 {
00063                         _start_time = std::clock(); 
00064                         paused = false;
00065                 }
00066                         
00067         }
00068                 
00069         void pause()
00070         {
00071                 if(!paused)
00072                 {
00073                         _elapsed += lastLap();
00074                         paused = true;
00075                 }
00076         }
00077         double elapsed() const
00078         {
00079                 if(paused)
00080                 {
00081                         return _elapsed;
00082                 }
00083                 else
00084                 {
00085                         return _elapsed + lastLap();
00086                 }
00087         }
00088 
00089         double lastLap() const                  // return elapsed time in seconds
00090         {
00091                 return  double(std::clock() - _start_time) / CLOCKS_PER_SEC; 
00092         }
00093 
00094         
00095 private:
00096         std::clock_t _start_time;
00097         double _elapsed;
00098         bool    paused;
00099         
00100 #else
00101         // Linux version
00102         public:
00103                 CPTimer()
00104                 { 
00105                         times(&_start_time);
00106                         _elapsed = 0.0;
00107                         paused = false;
00108                 }
00109         // postcondition: elapsed()==0
00110         
00111                 void   start()
00112                 { 
00113                         times(&_start_time);
00114                         _elapsed = 0.0;
00115                         paused = false;
00116                 } // post: elapsed()==0
00117         
00118                 void   restart()
00119                 {
00120                         times(&_start_time);
00121                         _elapsed = 0.0;
00122                         paused = false;
00123                 } // post: elapsed()==0
00124                 
00125                 void resume()
00126                 {
00127                         if(paused)
00128                         {
00129                                 times(&_start_time);
00130                                 paused = false;
00131                         }
00132                         
00133                 }
00134                 
00135                 void pause()
00136                 {
00137                         if(!paused)
00138                         {
00139                                 _elapsed += lastLap();
00140                                 paused = true;
00141                         }
00142                 }
00143                 double elapsed() const
00144                 {
00145                         if(paused)
00146                         {
00147                                 return _elapsed;
00148                         }
00149                         else
00150                         {
00151                                 return _elapsed + lastLap();
00152                         }
00153                 }
00154                 double lastLap() const                  // return elapsed time in seconds
00155                 {
00156                         struct tms now;
00157                         times(&now);
00158                         long int clk_tck = sysconf(_SC_CLK_TCK);
00159                         double utime = ((double)(now.tms_utime-_start_time.tms_utime))/clk_tck;
00160                         double stime = ((double)(now.tms_stime-_start_time.tms_stime))/clk_tck;
00161                         return  utime+stime;
00162                 }
00163         
00164         private:
00165                 struct tms _start_time;
00166                 double _elapsed;
00167                 bool    paused;
00168 #endif
00169 }; // timer
00170 
00171 
00172 #endif  // CPTIMER_H
00173 


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:28