clock.hxx

Go to the documentation of this file.
00001 
00002 
00003 #ifndef CH_TOOLS__CLOCK_HXX
00004 #define CH_TOOLS__CLOCK_HXX
00005 
00014 #include <iostream>
00015 #include <iomanip>
00016 extern "C"{
00017 #include <time.h>
00018 }
00019 #ifdef __unix
00020 extern "C"{
00021 #include <sys/resource.h>
00022 //we need extern int getrusage(int who, struct rusage *r_usage);
00023 }
00024 #endif
00025 
00028 namespace CH_Tools {
00029 
00035 
00045   class Microseconds{
00046     bool infinity;     
00047     long seconds;      
00048     long microsecs;    
00049 
00050 public:
00054 
00056     Microseconds(){seconds=0;microsecs=0;infinity=false;}
00058     Microseconds(bool infty){seconds=0;microsecs=0;infinity=infty;}
00060     Microseconds(const Microseconds& m)
00061     {seconds=m.seconds; microsecs=m.microsecs;infinity=m.infinity;}
00063     Microseconds(long secs, long msecs=0)
00064     {seconds=secs; microsecs=msecs;infinity=false;}
00065     
00067     Microseconds(int secs, int msecs=0)
00068     {seconds=secs; microsecs=msecs;infinity=false;}
00070     Microseconds(long hours,long minutes,long secs, long micros)
00071     {seconds=hours*3600+minutes*60+secs; microsecs=micros;infinity=false;}
00073     Microseconds(int hours,int minutes,int secs, int micros)
00074     {seconds=hours*3600+minutes*60+secs; microsecs=micros;infinity=false;}
00075 
00077 
00078     
00082 
00084     Microseconds& operator=(const Microseconds& m)
00085     {seconds=m.seconds; microsecs=m.microsecs; infinity=m.infinity;return *this;}
00086     
00088     Microseconds& operator+=(const Microseconds& m)
00089     {
00090       if (m.infinity){
00091         infinity=true;
00092         return *this;
00093       }
00094       microsecs+=m.microsecs;
00095       seconds+=m.seconds;
00096       while (microsecs>1000000){
00097         seconds++;
00098         microsecs-=1000000;
00099       }
00100       return *this;
00101     }
00102      
00104     Microseconds& operator-=(const Microseconds& m)
00105     {
00106      microsecs-=m.microsecs;
00107      seconds-=m.seconds;
00108      while (microsecs<0) {
00109          seconds--; microsecs+=1000000;
00110      }
00111      return *this;
00112     }
00113 
00115     Microseconds operator-(const Microseconds& m) const
00116     {Microseconds s(*this); return s-=m;}
00118     Microseconds operator+(const Microseconds& m) const
00119     {Microseconds s(*this); return s+=m;}
00120 
00122     bool operator<(const Microseconds& m) const
00123     {
00124       if (infinity) return false;
00125       if (m.infinity) return true;
00126       return ((seconds<m.seconds)||((seconds==m.seconds)&&(microsecs<m.microsecs)));
00127     }
00128 
00130     bool operator>(const Microseconds& m) const
00131     {
00132       if (infinity&&(!m.infinity)) return true;
00133       if (m.infinity) return false;
00134       return ((seconds>m.seconds)||((seconds==m.seconds)&&(microsecs>m.microsecs)));}
00135 
00137     bool operator<=(const Microseconds& m) const
00138     {
00139       if (infinity&&(!m.infinity)) return false;
00140       if (m.infinity) return true;
00141       return ((seconds<m.seconds)||((seconds==m.seconds)&&(microsecs<=m.microsecs)));}
00142 
00144     bool operator>=(const Microseconds& m) const 
00145     {
00146       if (infinity) return true;
00147       if (m.infinity) return false;
00148       return ((seconds>m.seconds)||((seconds==m.seconds)&&(microsecs>=m.microsecs)));}
00149 
00151     bool operator==(const Microseconds& m) const
00152     {
00153      if ((infinity&&(!m.infinity))||(m.infinity)) return false;
00154      return ((seconds==m.seconds)&&(microsecs==m.microsecs));}
00155 
00157 
00161 
00163     void set_infinity(bool infty){infinity=infty;}
00165     bool get_infinity() const {return infinity;}
00166 
00168     operator double() const 
00169     { return double(seconds)+double(microsecs)/1000000.; }
00170 
00172     void hhmmss(long& hours,long& minutes,long& secs) const
00173     {
00174      long s=seconds;
00175      if (microsecs>=500000) s++;
00176      hours=s/3600;
00177      minutes=(s%3600)/60;
00178      secs=(s%60);
00179     }
00180 
00182     void hhmmssdd(long& hours,long& minutes,long& secs,long& hund) const
00183     {
00184      hund=(microsecs+5000)/10000;
00185      long s=seconds;
00186      if (hund==100) {s++;hund=0;}
00187      hours=s/3600;
00188      minutes=(s%3600)/60;
00189      secs=(s%60);
00190     }
00191 
00193     long roundsecs() const
00194     {
00195      if (microsecs>=500000) return seconds+1;
00196      return seconds;
00197     }
00198     
00200     long roundhundredths() const
00201     {
00202      long hund=(microsecs+5000)/10000;
00203      hund+=seconds*100;
00204      return hund;
00205     }
00206 
00208 
00212 
00214     friend std::ostream& operator<<(std::ostream& out,const Microseconds& m)
00215     {if (m.infinity) return out<<"-1.000000";
00216      out<<m.seconds<<".";out.fill('0');out.width(6);out<<m.microsecs;
00217      out.fill(' ');return out;}
00218 
00219 
00221     friend std::istream& operator>>(std::istream& in,Microseconds& m)
00222     {
00223      char c; m.infinity=false;
00224      in>>m.seconds>>c>>m.microsecs;
00225      if (m.seconds<0) {m.seconds=0; m.infinity=true;}
00226      return in;
00227     }     
00228 
00230     friend void print_time(std::ostream& out,     
00231                            const Microseconds& m, 
00232                            int secondsonly=0      
00233                            )
00234     {
00235       long hours,minutes,seconds,hunds;
00236       out.fill('0');
00237       if (secondsonly){
00238         m.hhmmss(hours,minutes,seconds);
00239         out.width(2);out<<hours;
00240         out<<":";out.width(2);out<<minutes;
00241         out<<":";out.width(2);out<<seconds;
00242       }
00243       else {
00244         m.hhmmssdd(hours,minutes,seconds,hunds);
00245         out.width(2);out<<hours;
00246         out<<":";out.width(2);out<<minutes;
00247         out<<":";out.width(2);out<<seconds;
00248         out<<".";out.width(2);out<<hunds;
00249       }
00250       out.fill(' ');
00251     }
00252     
00254 
00255 };
00256 
00257 
00258 
00269 class Clock
00270 {
00271   private:
00272     Microseconds t_start;
00273     Microseconds offset;
00274 #ifdef __unix
00275 #else
00276     long l_start;
00277 #endif
00278 
00279   public:
00281     void start(){
00282 #ifdef __unix
00283         struct rusage r_usage;
00284         getrusage(RUSAGE_SELF,&r_usage);
00285         t_start=Microseconds(r_usage.ru_utime.tv_sec,r_usage.ru_utime.tv_usec);
00286         offset=Microseconds(0,0);
00287 #else
00288         t_start=Microseconds(static_cast<int>(::time(0)),0);
00289 #endif
00290     }
00291 
00293     Clock(){start();}
00295     ~Clock(){}
00296 
00298     void set_offset(Microseconds offs){
00299         offset=offs;
00300     }
00301 
00303     Microseconds time() const{
00304 #ifdef __unix
00305         struct rusage r_usage;
00306         getrusage(RUSAGE_SELF,&r_usage);
00307         Microseconds elapsed(r_usage.ru_utime.tv_sec,r_usage.ru_utime.tv_usec);
00308 #else
00309         Microseconds elapsed(static_cast<int>(::time(0)),0);
00310 #endif
00311         elapsed-=t_start;
00312         elapsed+=offset;
00313         return elapsed;
00314     }
00315 
00317     void elapsed_time(std::ostream& out) const
00318     {
00319         time_t current=::time(0);
00320         struct tm *loctime;                     
00321         Microseconds spent=time();
00322         out<<"elapsed time: ";
00323         print_time(out,spent);
00324         loctime=localtime(&current);
00325         out<<"   ----   "<<asctime(loctime)<<std::flush;
00326     }
00327 
00329     friend inline std::ostream& operator<<(std::ostream& out,const Clock& cl)
00330     {
00331         print_time(out,cl.time());
00332         return out;
00333     }
00334 };
00335 
00337 
00338 }
00339 
00340 #endif
00341 

Generated on Tue May 3 16:52:53 2011 for ConicBundle by  doxygen 1.5.6