stopwatch.h

Go to the documentation of this file.
00001 #ifndef STPWATCH_H
00002 #define STPWATCH_H
00003 
00004 #include  <ctime>
00005 #include  "gostream.h"
00006 #include  <unistd.h>
00007 #include <sys/resource.h>
00008 #include <time.h>
00009 
00010 /***********************************************************
00011  *****   ueberarbeitete und fehlerbereinigte Version   ***** 
00012  *****   Michael Schmich                  15.01.2002   *****
00013  ***********************************************************/
00014 
00015 /*----------------------------------------------------*/
00016 
00017 namespace Gascoigne
00018 {
00019 class Time
00020 {
00021  protected:
00022 
00023   double sec;
00024   int min, hour, day;
00025   
00026  public:
00027 
00028   Time() : sec(0.), min(0), hour(0),day(0) {}
00029 
00030   void reset() {sec=0.; min=hour=day=0;}
00031 
00032   double GetSeconds () const {return sec;}
00033   int    GetMinutes () const {return min;}
00034   int    GetHours   () const {return hour;}
00035   int    GetDays    () const {return day;}
00036 
00037   double GetTotalSeconds () const;
00038 
00039   void add(double s);
00040 
00041   friend std::ostream& operator<<(std::ostream &s, const Time& A);
00042 };
00043 
00044 /*----------------------------------------------------*/
00045 
00046 class StopWatch 
00047 {
00048  protected:
00049   bool     running;
00050   clock_t  last_time;
00051   Time     T;
00052   
00053  public:
00054   StopWatch();
00055 
00056   const Time& GetTime() const {return T;}
00057 
00058   void  add(double s) { T.add(s);}
00059   void  reset();
00060   void  start();
00061   double stop() ;
00062   double read() const;
00063 };
00064 
00065 /*----------------------------------------------------*/
00066 
00067           // measures the walltime
00068 class RTStopWatch 
00069 {
00070  protected:
00071   bool     running;
00072   timespec t1,t2;
00073   timespec my_time;
00074   
00075  public:
00076   RTStopWatch() 
00077     {
00078       reset();
00079     }
00080   
00081 
00082   void  reset()
00083     {
00084       running         = 0;
00085       my_time.tv_sec  = 0;
00086       my_time.tv_nsec = 0;
00087     }
00088   
00089   void  start()
00090     {
00091       if (!running) clock_gettime(CLOCK_REALTIME,&t1);
00092       running = 1;
00093     }
00094   
00095   double stop()
00096     {
00097       if (running)
00098         {
00099           clock_gettime(CLOCK_REALTIME,&t2);
00100           my_time.tv_sec  += t2.tv_sec-t1.tv_sec;
00101           my_time.tv_nsec += t2.tv_nsec-t1.tv_nsec;
00102 
00103           while (long(my_time.tv_nsec)>long(1000000000))
00104             {
00105               my_time.tv_nsec -= long(1000000000);
00106               my_time.tv_sec  += 1;
00107             }
00108           while (long(my_time.tv_nsec)<long(0))
00109             {
00110               my_time.tv_nsec += long(1000000000);
00111               my_time.tv_sec  -= 1;
00112             }
00113         }
00114       running = 0;
00115       return double( my_time.tv_sec)+1.e-9 * double (my_time.tv_nsec);
00116     }
00117   
00118   double read() const
00119     {
00120       if (running) return -1;
00121       return double (my_time.tv_sec)+1.e-9 * double (my_time.tv_nsec);
00122     }
00123   
00124 };
00125 
00126 
00127 
00128 }
00129 
00130 #endif

Generated on Thu Sep 14 10:34:35 2006 for Gascoigne by  doxygen 1.4.7