lemon/time_measure.h
changeset 1692 a34203867181
parent 1435 8e85e6bbefdf
child 1780 9f052750753f
equal deleted inserted replaced
0:5f0e507ea101 1:f6f2cbacd3a0
    48   ///
    48   ///
    49   ///\author Alpar Juttner
    49   ///\author Alpar Juttner
    50 
    50 
    51   class TimeStamp
    51   class TimeStamp
    52   {
    52   {
    53     tms ts;
    53     struct rtms 
       
    54     {
       
    55       double tms_utime;
       
    56       double tms_stime;
       
    57       double tms_cutime;
       
    58       double tms_cstime;
       
    59       rtms() {}
       
    60       rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
       
    61 		     tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
       
    62     };
       
    63     rtms ts;
    54     double real_time;
    64     double real_time;
    55   
    65   
       
    66     rtms &getTms() {return ts;}
       
    67     const rtms &getTms() const {return ts;}
       
    68 
    56   public:
    69   public:
    57 
    70 
    58     tms &getTms() {return ts;}
       
    59     const tms &getTms() const {return ts;}
       
    60     ///Read the current time values of the process
    71     ///Read the current time values of the process
    61     void stamp()
    72     void stamp()
    62     {
    73     {
    63       timeval tv;
    74       timeval tv;
    64       times(&ts);
    75       tms _ts;
       
    76       times(&_ts);
    65       gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
    77       gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
       
    78       ts=_ts;
    66     }
    79     }
    67   
    80   
    68     /// Constructor initializing with zero
    81     /// Constructor initializing with zero
    69     TimeStamp()
    82     TimeStamp()
    70     { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
    83     { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
   101     TimeStamp operator-(const TimeStamp &b) const
   114     TimeStamp operator-(const TimeStamp &b) const
   102     {
   115     {
   103       TimeStamp t(*this);
   116       TimeStamp t(*this);
   104       return t-=b;
   117       return t-=b;
   105     }
   118     }
       
   119     ///\e
       
   120 
       
   121     ///\bug operator * and / gives rounded values!
       
   122     TimeStamp &operator*=(double b)
       
   123     {
       
   124       ts.tms_utime*=b;
       
   125       ts.tms_stime*=b;
       
   126       ts.tms_cutime*=b;
       
   127       ts.tms_cstime*=b;
       
   128       real_time*=b;
       
   129       return *this;
       
   130     }
       
   131     ///\e
       
   132     TimeStamp operator*(double b) const
       
   133     {
       
   134       TimeStamp t(*this);
       
   135       return t*=b;
       
   136     }
       
   137     friend TimeStamp operator*(double b,const TimeStamp &t);
       
   138     ///\e
       
   139     TimeStamp &operator/=(double b)
       
   140     {
       
   141       ts.tms_utime/=b;
       
   142       ts.tms_stime/=b;
       
   143       ts.tms_cutime/=b;
       
   144       ts.tms_cstime/=b;
       
   145       real_time/=b;
       
   146       return *this;
       
   147     }
       
   148     ///\e
       
   149     TimeStamp operator/(double b) const
       
   150     {
       
   151       TimeStamp t(*this);
       
   152       return t/=b;
       
   153     }
   106     ///The time ellapsed since the last call of stamp()
   154     ///The time ellapsed since the last call of stamp()
   107     TimeStamp ellapsed() const
   155     TimeStamp ellapsed() const
   108     {
   156     {
   109       TimeStamp t(NULL);
   157       TimeStamp t(NULL);
   110       return t-*this;
   158       return t-*this;
   111     }
   159     }
   112   
   160   
   113     friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
   161     friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
   114   
   162   
   115     ///Gives back the user time of the process
   163     ///Gives back the user time of the process
   116     double getUserTime() const
   164     double userTime() const
   117     {
   165     {
   118       return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
   166       return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
   119     }
   167     }
   120     ///Gives back the system time of the process
   168     ///Gives back the system time of the process
   121     double getSystemTime() const
   169     double systemTime() const
   122     {
   170     {
   123       return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
   171       return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
   124     }
   172     }
   125     ///Gives back the user time of the process' children
   173     ///Gives back the user time of the process' children
   126     double getCUserTime() const
   174     double cUserTime() const
   127     {
   175     {
   128       return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
   176       return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
   129     }
   177     }
   130     ///Gives back the user time of the process' children
   178     ///Gives back the user time of the process' children
   131     double getCSystemTime() const
   179     double cSystemTime() const
   132     {
   180     {
   133       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   181       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   134     }
   182     }
   135     ///Gives back the real time of the process
   183     ///Gives back the real time of the process
   136     double getRealTime() const {return real_time;}
   184     double realTime() const {return real_time;}
   137   };
   185   };
   138 
   186 
       
   187   TimeStamp operator*(double b,const TimeStamp &t) 
       
   188   {
       
   189     return t*b;
       
   190   }
       
   191   
   139   ///Class measuring the cpu time and real time usage of the process
   192   ///Class measuring the cpu time and real time usage of the process
   140 
   193 
   141   ///Class measuring the cpu time and real time usage of the process.
   194   ///Class measuring the cpu time and real time usage of the process.
   142   ///It is quite easy-to-use, here is a short example.
   195   ///It is quite easy-to-use, here is a short example.
   143   ///\code
   196   ///\code
   195       _reset();
   248       _reset();
   196     }
   249     }
   197 
   250 
   198 
   251 
   199     ///Gives back the ellapsed user time of the process
   252     ///Gives back the ellapsed user time of the process
   200     double getUserTime() const
   253     double userTime() const
   201     {
   254     {
   202       return operator TimeStamp().getUserTime();
   255       return operator TimeStamp().userTime();
   203     }
   256     }
   204     ///Gives back the ellapsed system time of the process
   257     ///Gives back the ellapsed system time of the process
   205     double getSystemTime() const
   258     double systemTime() const
   206     {
   259     {
   207       return operator TimeStamp().getSystemTime();
   260       return operator TimeStamp().systemTime();
   208     }
   261     }
   209     ///Gives back the ellapsed user time of the process' children
   262     ///Gives back the ellapsed user time of the process' children
   210     double getCUserTime() const
   263     double cUserTime() const
   211     {
   264     {
   212       return operator TimeStamp().getCUserTime();
   265       return operator TimeStamp().cUserTime();
   213     }
   266     }
   214     ///Gives back the ellapsed user time of the process' children
   267     ///Gives back the ellapsed user time of the process' children
   215     double getCSystemTime() const
   268     double cSystemTime() const
   216     {
   269     {
   217       return operator TimeStamp().getCSystemTime();
   270       return operator TimeStamp().cSystemTime();
   218     }
   271     }
   219     ///Gives back the ellapsed real time of the process
   272     ///Gives back the ellapsed real time of the process
   220     double getRealTime() const
   273     double realTime() const
   221     {
   274     {
   222       return operator TimeStamp().getRealTime();
   275       return operator TimeStamp().realTime();
   223     }
   276     }
   224 
   277 
   225   };
   278   };
   226 
   279 
   227   ///Prints the time counters
   280   ///Prints the time counters
   242     long cls = sysconf(_SC_CLK_TCK);
   295     long cls = sysconf(_SC_CLK_TCK);
   243     os << "u: " << double(t.getTms().tms_utime)/cls <<
   296     os << "u: " << double(t.getTms().tms_utime)/cls <<
   244       "s, s: " << double(t.getTms().tms_stime)/cls <<
   297       "s, s: " << double(t.getTms().tms_stime)/cls <<
   245       "s, cu: " << double(t.getTms().tms_cutime)/cls <<
   298       "s, cu: " << double(t.getTms().tms_cutime)/cls <<
   246       "s, cs: " << double(t.getTms().tms_cstime)/cls <<
   299       "s, cs: " << double(t.getTms().tms_cstime)/cls <<
   247       "s, real: " << t.getRealTime() << "s";
   300       "s, real: " << t.realTime() << "s";
   248     return os;
   301     return os;
   249   }
   302   }
   250 
   303 
       
   304   
       
   305   ///Tool to measure the running time more exactly.
       
   306   
       
   307   ///This function calls \c f several times and returns the average
       
   308   ///running time. The number of the executions will be choosen in such a way
       
   309   ///that the full running time will be roughly between \c min_time
       
   310   ///and <tt>2*min_time</tt>.
       
   311   ///\param f the function object to be measured.
       
   312   ///\param min_time the minimum total running time.
       
   313   ///\retval num if it is not \c NULL, then *num will contain the actual
       
   314   ///        number of execution of \c f.
       
   315   ///\retval full_time if it is not \c NULL, then *full_time
       
   316   ///        will contain the actual
       
   317   ///        total running time.
       
   318   ///\return The average running time of \c f.
       
   319   
       
   320   template<class F>
       
   321   TimeStamp runningTimeTest(F &f,double min_time=10,int *num = NULL,
       
   322 			TimeStamp *full_time=NULL)
       
   323   {
       
   324     Timer t;
       
   325     TimeStamp full;
       
   326     int total=0;
       
   327     for(int tn=1;tn < 1<<24; tn*=2) {
       
   328       for(;total<tn;total++) f();
       
   329       full=t;
       
   330       if(full.realTime()>min_time) {
       
   331 	if(num) *num=total;
       
   332 	if(full_time) *full_time=full;
       
   333       return full/total;
       
   334       }
       
   335     }
       
   336     return TimeStamp();
       
   337   }
       
   338   
   251   /// @}  
   339   /// @}  
   252 
   340 
       
   341 
   253 } //namespace lemon
   342 } //namespace lemon
   254 
   343 
   255 #endif //LEMON_TIME_MEASURE_H
   344 #endif //LEMON_TIME_MEASURE_H