lemon/time_measure.h
changeset 1689 f1795dafe42c
parent 1435 8e85e6bbefdf
child 1780 9f052750753f
     1.1 --- a/lemon/time_measure.h	Fri Sep 16 09:57:02 2005 +0000
     1.2 +++ b/lemon/time_measure.h	Wed Sep 28 08:14:39 2005 +0000
     1.3 @@ -50,19 +50,32 @@
     1.4  
     1.5    class TimeStamp
     1.6    {
     1.7 -    tms ts;
     1.8 +    struct rtms 
     1.9 +    {
    1.10 +      double tms_utime;
    1.11 +      double tms_stime;
    1.12 +      double tms_cutime;
    1.13 +      double tms_cstime;
    1.14 +      rtms() {}
    1.15 +      rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
    1.16 +		     tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
    1.17 +    };
    1.18 +    rtms ts;
    1.19      double real_time;
    1.20    
    1.21 +    rtms &getTms() {return ts;}
    1.22 +    const rtms &getTms() const {return ts;}
    1.23 +
    1.24    public:
    1.25  
    1.26 -    tms &getTms() {return ts;}
    1.27 -    const tms &getTms() const {return ts;}
    1.28      ///Read the current time values of the process
    1.29      void stamp()
    1.30      {
    1.31        timeval tv;
    1.32 -      times(&ts);
    1.33 +      tms _ts;
    1.34 +      times(&_ts);
    1.35        gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
    1.36 +      ts=_ts;
    1.37      }
    1.38    
    1.39      /// Constructor initializing with zero
    1.40 @@ -103,6 +116,41 @@
    1.41        TimeStamp t(*this);
    1.42        return t-=b;
    1.43      }
    1.44 +    ///\e
    1.45 +
    1.46 +    ///\bug operator * and / gives rounded values!
    1.47 +    TimeStamp &operator*=(double b)
    1.48 +    {
    1.49 +      ts.tms_utime*=b;
    1.50 +      ts.tms_stime*=b;
    1.51 +      ts.tms_cutime*=b;
    1.52 +      ts.tms_cstime*=b;
    1.53 +      real_time*=b;
    1.54 +      return *this;
    1.55 +    }
    1.56 +    ///\e
    1.57 +    TimeStamp operator*(double b) const
    1.58 +    {
    1.59 +      TimeStamp t(*this);
    1.60 +      return t*=b;
    1.61 +    }
    1.62 +    friend TimeStamp operator*(double b,const TimeStamp &t);
    1.63 +    ///\e
    1.64 +    TimeStamp &operator/=(double b)
    1.65 +    {
    1.66 +      ts.tms_utime/=b;
    1.67 +      ts.tms_stime/=b;
    1.68 +      ts.tms_cutime/=b;
    1.69 +      ts.tms_cstime/=b;
    1.70 +      real_time/=b;
    1.71 +      return *this;
    1.72 +    }
    1.73 +    ///\e
    1.74 +    TimeStamp operator/(double b) const
    1.75 +    {
    1.76 +      TimeStamp t(*this);
    1.77 +      return t/=b;
    1.78 +    }
    1.79      ///The time ellapsed since the last call of stamp()
    1.80      TimeStamp ellapsed() const
    1.81      {
    1.82 @@ -113,29 +161,34 @@
    1.83      friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
    1.84    
    1.85      ///Gives back the user time of the process
    1.86 -    double getUserTime() const
    1.87 +    double userTime() const
    1.88      {
    1.89        return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
    1.90      }
    1.91      ///Gives back the system time of the process
    1.92 -    double getSystemTime() const
    1.93 +    double systemTime() const
    1.94      {
    1.95        return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
    1.96      }
    1.97      ///Gives back the user time of the process' children
    1.98 -    double getCUserTime() const
    1.99 +    double cUserTime() const
   1.100      {
   1.101        return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
   1.102      }
   1.103      ///Gives back the user time of the process' children
   1.104 -    double getCSystemTime() const
   1.105 +    double cSystemTime() const
   1.106      {
   1.107        return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   1.108      }
   1.109      ///Gives back the real time of the process
   1.110 -    double getRealTime() const {return real_time;}
   1.111 +    double realTime() const {return real_time;}
   1.112    };
   1.113  
   1.114 +  TimeStamp operator*(double b,const TimeStamp &t) 
   1.115 +  {
   1.116 +    return t*b;
   1.117 +  }
   1.118 +  
   1.119    ///Class measuring the cpu time and real time usage of the process
   1.120  
   1.121    ///Class measuring the cpu time and real time usage of the process.
   1.122 @@ -197,29 +250,29 @@
   1.123  
   1.124  
   1.125      ///Gives back the ellapsed user time of the process
   1.126 -    double getUserTime() const
   1.127 +    double userTime() const
   1.128      {
   1.129 -      return operator TimeStamp().getUserTime();
   1.130 +      return operator TimeStamp().userTime();
   1.131      }
   1.132      ///Gives back the ellapsed system time of the process
   1.133 -    double getSystemTime() const
   1.134 +    double systemTime() const
   1.135      {
   1.136 -      return operator TimeStamp().getSystemTime();
   1.137 +      return operator TimeStamp().systemTime();
   1.138      }
   1.139      ///Gives back the ellapsed user time of the process' children
   1.140 -    double getCUserTime() const
   1.141 +    double cUserTime() const
   1.142      {
   1.143 -      return operator TimeStamp().getCUserTime();
   1.144 +      return operator TimeStamp().cUserTime();
   1.145      }
   1.146      ///Gives back the ellapsed user time of the process' children
   1.147 -    double getCSystemTime() const
   1.148 +    double cSystemTime() const
   1.149      {
   1.150 -      return operator TimeStamp().getCSystemTime();
   1.151 +      return operator TimeStamp().cSystemTime();
   1.152      }
   1.153      ///Gives back the ellapsed real time of the process
   1.154 -    double getRealTime() const
   1.155 +    double realTime() const
   1.156      {
   1.157 -      return operator TimeStamp().getRealTime();
   1.158 +      return operator TimeStamp().realTime();
   1.159      }
   1.160  
   1.161    };
   1.162 @@ -244,12 +297,48 @@
   1.163        "s, s: " << double(t.getTms().tms_stime)/cls <<
   1.164        "s, cu: " << double(t.getTms().tms_cutime)/cls <<
   1.165        "s, cs: " << double(t.getTms().tms_cstime)/cls <<
   1.166 -      "s, real: " << t.getRealTime() << "s";
   1.167 +      "s, real: " << t.realTime() << "s";
   1.168      return os;
   1.169    }
   1.170  
   1.171 +  
   1.172 +  ///Tool to measure the running time more exactly.
   1.173 +  
   1.174 +  ///This function calls \c f several times and returns the average
   1.175 +  ///running time. The number of the executions will be choosen in such a way
   1.176 +  ///that the full running time will be roughly between \c min_time
   1.177 +  ///and <tt>2*min_time</tt>.
   1.178 +  ///\param f the function object to be measured.
   1.179 +  ///\param min_time the minimum total running time.
   1.180 +  ///\retval num if it is not \c NULL, then *num will contain the actual
   1.181 +  ///        number of execution of \c f.
   1.182 +  ///\retval full_time if it is not \c NULL, then *full_time
   1.183 +  ///        will contain the actual
   1.184 +  ///        total running time.
   1.185 +  ///\return The average running time of \c f.
   1.186 +  
   1.187 +  template<class F>
   1.188 +  TimeStamp runningTimeTest(F &f,double min_time=10,int *num = NULL,
   1.189 +			TimeStamp *full_time=NULL)
   1.190 +  {
   1.191 +    Timer t;
   1.192 +    TimeStamp full;
   1.193 +    int total=0;
   1.194 +    for(int tn=1;tn < 1<<24; tn*=2) {
   1.195 +      for(;total<tn;total++) f();
   1.196 +      full=t;
   1.197 +      if(full.realTime()>min_time) {
   1.198 +	if(num) *num=total;
   1.199 +	if(full_time) *full_time=full;
   1.200 +      return full/total;
   1.201 +      }
   1.202 +    }
   1.203 +    return TimeStamp();
   1.204 +  }
   1.205 +  
   1.206    /// @}  
   1.207  
   1.208 +
   1.209  } //namespace lemon
   1.210  
   1.211  #endif //LEMON_TIME_MEASURE_H