- runningTimeTest(): a tool to measure running times more precisely.
authoralpar
Wed, 28 Sep 2005 08:14:39 +0000
changeset 1689f1795dafe42c
parent 1688 61ce46476787
child 1690 7db44a7ab939
- runningTimeTest(): a tool to measure running times more precisely.
- TimeStamp now uses double to count cpu-times
- 'get's removed from the query functions of Times and TimeStamp
benchmark/bench_tools.h
benchmark/bfs-bench.cc
benchmark/hcube.cc
lemon/time_measure.h
test/time_measure_test.cc
     1.1 --- a/benchmark/bench_tools.h	Fri Sep 16 09:57:02 2005 +0000
     1.2 +++ b/benchmark/bench_tools.h	Wed Sep 28 08:14:39 2005 +0000
     1.3 @@ -83,15 +83,15 @@
     1.4  inline void PrintTime(char *ID,lemon::Timer &T) 
     1.5  {
     1.6    lemon::TimeStamp S(T);
     1.7 -  std::cout << ID << ' ' << S.getUserTime() << ' '
     1.8 -	    << S.getSystemTime() << ' ' << S.getRealTime() << std::endl;
     1.9 +  std::cout << ID << ' ' << S.userTime() << ' '
    1.10 +	    << S.systemTime() << ' ' << S.realTime() << std::endl;
    1.11  }
    1.12  
    1.13  
    1.14  
    1.15  ///
    1.16  template<class Graph>
    1.17 -void addHiperCube(Graph &G,int dim,std::vector<typename Graph::Node> &nodes)
    1.18 +void addHyperCube(Graph &G,int dim,std::vector<typename Graph::Node> &nodes)
    1.19  {
    1.20    GRAPH_TYPEDEF_FACTORY(Graph);
    1.21    
    1.22 @@ -107,7 +107,7 @@
    1.23  
    1.24  ///
    1.25  template<class Graph>
    1.26 -void addBiDirHiperCube(Graph &G,int dim,std::vector<typename Graph::Node>&nodes)
    1.27 +void addBiDirHyperCube(Graph &G,int dim,std::vector<typename Graph::Node>&nodes)
    1.28  {
    1.29    GRAPH_TYPEDEF_FACTORY(Graph);
    1.30    
     2.1 --- a/benchmark/bfs-bench.cc	Fri Sep 16 09:57:02 2005 +0000
     2.2 +++ b/benchmark/bfs-bench.cc	Wed Sep 28 08:14:39 2005 +0000
     2.3 @@ -117,7 +117,7 @@
     2.4  
     2.5    T.reset();
     2.6    vector<Node> nodes;
     2.7 -  addBiDirHiperCube(G,dim,nodes);
     2.8 +  addBiDirHyperCube(G,dim,nodes);
     2.9  
    2.10    PrintTime("GENGRAPH",T);
    2.11  
     3.1 --- a/benchmark/hcube.cc	Fri Sep 16 09:57:02 2005 +0000
     3.2 +++ b/benchmark/hcube.cc	Wed Sep 28 08:14:39 2005 +0000
     3.3 @@ -55,7 +55,7 @@
     3.4  
     3.5    T.reset();
     3.6    vector<Node> nodes;
     3.7 -  addBiDirHiperCube(G,dim,nodes);
     3.8 +  addBiDirHyperCube(G,dim,nodes);
     3.9  
    3.10    PrintTime("GENGRAPH",T);
    3.11  
     4.1 --- a/lemon/time_measure.h	Fri Sep 16 09:57:02 2005 +0000
     4.2 +++ b/lemon/time_measure.h	Wed Sep 28 08:14:39 2005 +0000
     4.3 @@ -50,19 +50,32 @@
     4.4  
     4.5    class TimeStamp
     4.6    {
     4.7 -    tms ts;
     4.8 +    struct rtms 
     4.9 +    {
    4.10 +      double tms_utime;
    4.11 +      double tms_stime;
    4.12 +      double tms_cutime;
    4.13 +      double tms_cstime;
    4.14 +      rtms() {}
    4.15 +      rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
    4.16 +		     tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
    4.17 +    };
    4.18 +    rtms ts;
    4.19      double real_time;
    4.20    
    4.21 +    rtms &getTms() {return ts;}
    4.22 +    const rtms &getTms() const {return ts;}
    4.23 +
    4.24    public:
    4.25  
    4.26 -    tms &getTms() {return ts;}
    4.27 -    const tms &getTms() const {return ts;}
    4.28      ///Read the current time values of the process
    4.29      void stamp()
    4.30      {
    4.31        timeval tv;
    4.32 -      times(&ts);
    4.33 +      tms _ts;
    4.34 +      times(&_ts);
    4.35        gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
    4.36 +      ts=_ts;
    4.37      }
    4.38    
    4.39      /// Constructor initializing with zero
    4.40 @@ -103,6 +116,41 @@
    4.41        TimeStamp t(*this);
    4.42        return t-=b;
    4.43      }
    4.44 +    ///\e
    4.45 +
    4.46 +    ///\bug operator * and / gives rounded values!
    4.47 +    TimeStamp &operator*=(double b)
    4.48 +    {
    4.49 +      ts.tms_utime*=b;
    4.50 +      ts.tms_stime*=b;
    4.51 +      ts.tms_cutime*=b;
    4.52 +      ts.tms_cstime*=b;
    4.53 +      real_time*=b;
    4.54 +      return *this;
    4.55 +    }
    4.56 +    ///\e
    4.57 +    TimeStamp operator*(double b) const
    4.58 +    {
    4.59 +      TimeStamp t(*this);
    4.60 +      return t*=b;
    4.61 +    }
    4.62 +    friend TimeStamp operator*(double b,const TimeStamp &t);
    4.63 +    ///\e
    4.64 +    TimeStamp &operator/=(double b)
    4.65 +    {
    4.66 +      ts.tms_utime/=b;
    4.67 +      ts.tms_stime/=b;
    4.68 +      ts.tms_cutime/=b;
    4.69 +      ts.tms_cstime/=b;
    4.70 +      real_time/=b;
    4.71 +      return *this;
    4.72 +    }
    4.73 +    ///\e
    4.74 +    TimeStamp operator/(double b) const
    4.75 +    {
    4.76 +      TimeStamp t(*this);
    4.77 +      return t/=b;
    4.78 +    }
    4.79      ///The time ellapsed since the last call of stamp()
    4.80      TimeStamp ellapsed() const
    4.81      {
    4.82 @@ -113,29 +161,34 @@
    4.83      friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
    4.84    
    4.85      ///Gives back the user time of the process
    4.86 -    double getUserTime() const
    4.87 +    double userTime() const
    4.88      {
    4.89        return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
    4.90      }
    4.91      ///Gives back the system time of the process
    4.92 -    double getSystemTime() const
    4.93 +    double systemTime() const
    4.94      {
    4.95        return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
    4.96      }
    4.97      ///Gives back the user time of the process' children
    4.98 -    double getCUserTime() const
    4.99 +    double cUserTime() const
   4.100      {
   4.101        return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
   4.102      }
   4.103      ///Gives back the user time of the process' children
   4.104 -    double getCSystemTime() const
   4.105 +    double cSystemTime() const
   4.106      {
   4.107        return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   4.108      }
   4.109      ///Gives back the real time of the process
   4.110 -    double getRealTime() const {return real_time;}
   4.111 +    double realTime() const {return real_time;}
   4.112    };
   4.113  
   4.114 +  TimeStamp operator*(double b,const TimeStamp &t) 
   4.115 +  {
   4.116 +    return t*b;
   4.117 +  }
   4.118 +  
   4.119    ///Class measuring the cpu time and real time usage of the process
   4.120  
   4.121    ///Class measuring the cpu time and real time usage of the process.
   4.122 @@ -197,29 +250,29 @@
   4.123  
   4.124  
   4.125      ///Gives back the ellapsed user time of the process
   4.126 -    double getUserTime() const
   4.127 +    double userTime() const
   4.128      {
   4.129 -      return operator TimeStamp().getUserTime();
   4.130 +      return operator TimeStamp().userTime();
   4.131      }
   4.132      ///Gives back the ellapsed system time of the process
   4.133 -    double getSystemTime() const
   4.134 +    double systemTime() const
   4.135      {
   4.136 -      return operator TimeStamp().getSystemTime();
   4.137 +      return operator TimeStamp().systemTime();
   4.138      }
   4.139      ///Gives back the ellapsed user time of the process' children
   4.140 -    double getCUserTime() const
   4.141 +    double cUserTime() const
   4.142      {
   4.143 -      return operator TimeStamp().getCUserTime();
   4.144 +      return operator TimeStamp().cUserTime();
   4.145      }
   4.146      ///Gives back the ellapsed user time of the process' children
   4.147 -    double getCSystemTime() const
   4.148 +    double cSystemTime() const
   4.149      {
   4.150 -      return operator TimeStamp().getCSystemTime();
   4.151 +      return operator TimeStamp().cSystemTime();
   4.152      }
   4.153      ///Gives back the ellapsed real time of the process
   4.154 -    double getRealTime() const
   4.155 +    double realTime() const
   4.156      {
   4.157 -      return operator TimeStamp().getRealTime();
   4.158 +      return operator TimeStamp().realTime();
   4.159      }
   4.160  
   4.161    };
   4.162 @@ -244,12 +297,48 @@
   4.163        "s, s: " << double(t.getTms().tms_stime)/cls <<
   4.164        "s, cu: " << double(t.getTms().tms_cutime)/cls <<
   4.165        "s, cs: " << double(t.getTms().tms_cstime)/cls <<
   4.166 -      "s, real: " << t.getRealTime() << "s";
   4.167 +      "s, real: " << t.realTime() << "s";
   4.168      return os;
   4.169    }
   4.170  
   4.171 +  
   4.172 +  ///Tool to measure the running time more exactly.
   4.173 +  
   4.174 +  ///This function calls \c f several times and returns the average
   4.175 +  ///running time. The number of the executions will be choosen in such a way
   4.176 +  ///that the full running time will be roughly between \c min_time
   4.177 +  ///and <tt>2*min_time</tt>.
   4.178 +  ///\param f the function object to be measured.
   4.179 +  ///\param min_time the minimum total running time.
   4.180 +  ///\retval num if it is not \c NULL, then *num will contain the actual
   4.181 +  ///        number of execution of \c f.
   4.182 +  ///\retval full_time if it is not \c NULL, then *full_time
   4.183 +  ///        will contain the actual
   4.184 +  ///        total running time.
   4.185 +  ///\return The average running time of \c f.
   4.186 +  
   4.187 +  template<class F>
   4.188 +  TimeStamp runningTimeTest(F &f,double min_time=10,int *num = NULL,
   4.189 +			TimeStamp *full_time=NULL)
   4.190 +  {
   4.191 +    Timer t;
   4.192 +    TimeStamp full;
   4.193 +    int total=0;
   4.194 +    for(int tn=1;tn < 1<<24; tn*=2) {
   4.195 +      for(;total<tn;total++) f();
   4.196 +      full=t;
   4.197 +      if(full.realTime()>min_time) {
   4.198 +	if(num) *num=total;
   4.199 +	if(full_time) *full_time=full;
   4.200 +      return full/total;
   4.201 +      }
   4.202 +    }
   4.203 +    return TimeStamp();
   4.204 +  }
   4.205 +  
   4.206    /// @}  
   4.207  
   4.208 +
   4.209  } //namespace lemon
   4.210  
   4.211  #endif //LEMON_TIME_MEASURE_H
     5.1 --- a/test/time_measure_test.cc	Fri Sep 16 09:57:02 2005 +0000
     5.2 +++ b/test/time_measure_test.cc	Wed Sep 28 08:14:39 2005 +0000
     5.3 @@ -23,14 +23,26 @@
     5.4  
     5.5  using namespace lemon;
     5.6  
     5.7 +void f() 
     5.8 +{
     5.9 +  double d=0;
    5.10 +  for(int i=0;i<10000;i++)
    5.11 +    d+=0.1;
    5.12 +}
    5.13 +
    5.14  int main()
    5.15  {
    5.16    Timer T;
    5.17 -  while(T.getRealTime()<1.0) ;
    5.18 +  int n;
    5.19 +  for(n=0;T.realTime()<1.0;n++) ;
    5.20 +  std::cout << T << " (" << n << " time queries)\n";
    5.21 +  T.reset();
    5.22 +  while(T.realTime()<2.0) ;
    5.23    std::cout << T << '\n';
    5.24 -  T.reset();
    5.25 -  while(T.getRealTime()<2.0) ;
    5.26 -  std::cout << T << '\n';
    5.27 +  TimeStamp full;
    5.28 +  TimeStamp t=runningTimeTest(f,1,&n,&full);
    5.29 +  std::cout << t << " (" << n << " tests)\n";
    5.30 +  std::cout << "Total: " << full << "\n";
    5.31    
    5.32    return 0;
    5.33  }