marci@73: // -*- c++ -*-
marci@259: #ifndef HUGO_TIME_MEASURE_H
marci@259: #define HUGO_TIME_MEASURE_H
marci@73: 
marci@73: #include <sys/time.h>
alpar@117: #include <sys/times.h>
alpar@117: #include <fstream>
alpar@117: #include <iostream>
jacint@121: #include <unistd.h>
marci@73: 
marci@323: namespace hugo {
marci@73: 
marci@323: //   double currTime() {
marci@323: //     timeval tv;
marci@323: //     //timezone tz;
marci@323: //     gettimeofday(&tv, 0);
marci@323: //     return double(tv.tv_sec)+double(tv.tv_usec)/1000000.0;
marci@323: //   }
marci@323: 
alpar@327:   /// Class to store (cpu)time instances.
alpar@327: 
alpar@327:   /// This class stores five time values.
alpar@327:   /// - a real time
alpar@327:   /// - a user cpu time
alpar@327:   /// - a system cpu time
alpar@327:   /// - a user cpu time of children
alpar@327:   /// - a system cpu time of children
alpar@327:   ///
alpar@327:   /// TimeStamp's can be added to or substracted from each other and
alpar@327:   /// they can be push to a stream.
alpar@327: 
marci@323:   class TimeStamp
marci@323:   {
marci@323:     tms ts;
marci@323:     double real_time;
alpar@118:   
marci@323:   public:
alpar@117: 
marci@323:     tms &getTms() {return ts;}
marci@323:     const tms &getTms() const {return ts;}
marci@323:     double getRealTime() const {return real_time;}
alpar@327:     ///Read the current time values of the process.
marci@323:     void stamp()
marci@323:     {
marci@323:       timeval tv;
marci@323:       times(&ts);
marci@323:       gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
marci@323:     }
marci@323:   
alpar@327:     /// Constructor initializing with zero.
marci@323:     TimeStamp()
marci@323:     { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
alpar@327:     ///Constructor initializing with the current time values of the process.
marci@323:     TimeStamp(void *) { stamp();}
marci@323:   
marci@323:     TimeStamp &operator+=(const TimeStamp &b)
marci@323:     {
marci@323:       ts.tms_utime+=b.ts.tms_utime;
marci@323:       ts.tms_stime+=b.ts.tms_stime;
marci@323:       ts.tms_cutime+=b.ts.tms_cutime;
marci@323:       ts.tms_cstime+=b.ts.tms_cstime;
marci@323:       real_time+=b.real_time;
marci@323:       return *this;
marci@323:     }
marci@323:     TimeStamp operator+(const TimeStamp &b) const
marci@323:     {
marci@323:       TimeStamp t(*this);
marci@323:       return t+=b;
marci@323:     }
marci@323:     TimeStamp &operator-=(const TimeStamp &b)
marci@323:     {
marci@323:       ts.tms_utime-=b.ts.tms_utime;
marci@323:       ts.tms_stime-=b.ts.tms_stime;
marci@323:       ts.tms_cutime-=b.ts.tms_cutime;
marci@323:       ts.tms_cstime-=b.ts.tms_cstime;
marci@323:       real_time-=b.real_time;
marci@323:       return *this;
marci@323:     }
marci@323:     TimeStamp operator-(const TimeStamp &b) const
marci@323:     {
marci@323:       TimeStamp t(*this);
marci@323:       return t-=b;
marci@323:     }
marci@323: 
alpar@327:     ///The time ellapsed since the last call of stamp()
alpar@324:     TimeStamp ellapsed() const
marci@323:     {
marci@323:       TimeStamp t(NULL);
marci@323:       return t-*this;
marci@323:     }
marci@323:   
marci@323:     friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
marci@323:   
marci@323:     double getUserTime() const
marci@323:     {
marci@323:       return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
marci@323:     }
marci@323:     double getSystemTime() const
marci@323:     {
marci@323:       return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
marci@323:     }
marci@323:     double getCUserTime() const
marci@323:     {
marci@323:       return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
marci@323:     }
marci@323:     double getCSystemTime() const
marci@323:     {
marci@323:       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
marci@323:     }
marci@323:   };
marci@323: 
alpar@327:   ///Class measuring the cpu time and real time usage of the process.
marci@323:   class Timer
alpar@118:   {
marci@323:     TimeStamp start_time;
marci@323: 
marci@323:     void _reset() {start_time.stamp();}
alpar@118:   
marci@323:   public: 
alpar@327:     ///Constructor. It starts with zero time counters.
marci@323:     Timer() {_reset();}
marci@323: 
alpar@327:     ///Computes the ellapsed time.
alpar@327: 
alpar@327:     ///This conversion computes the ellapsed time
alpar@327:     ///since the construction of \c t or since
alpar@327:     ///the last \c t.reset().
marci@323:     operator TimeStamp ()
marci@323:     {
marci@323:       TimeStamp t;
marci@323:       t.stamp();
marci@323:       return t-start_time;
marci@323:     }
marci@323: 
alpar@327:     ///Resets the time counters.
marci@323:     TimeStamp reset()
marci@323:     {
marci@323:       TimeStamp t(start_time);
marci@323:       _reset();
marci@323:       return start_time-t;
marci@323:     }
marci@323:   };
marci@323: 
alpar@327:   ///Prints the time counters.
marci@323:   inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
alpar@117:   {
marci@323:     long cls = sysconf(_SC_CLK_TCK);
marci@323:     os << "u: " << double(t.getTms().tms_utime)/cls <<
marci@323:       "s, s: " << double(t.getTms().tms_stime)/cls <<
marci@323:       "s, cu: " << double(t.getTms().tms_cutime)/cls <<
marci@323:       "s, cs: " << double(t.getTms().tms_cstime)/cls <<
marci@323:       "s, real: " << t.getRealTime() << "s";
marci@323:     return os;
alpar@117:   }
alpar@117: 
marci@323: } //namespace hugo
alpar@117: 
marci@259: #endif //HUGO_TIME_MEASURE_H