[Lemon-commits] [lemon_svn] alpar: r2213 - in hugo/trunk: benchmark lemon test

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:50:58 CET 2006


Author: alpar
Date: Wed Sep 28 10:14:39 2005
New Revision: 2213

Modified:
   hugo/trunk/benchmark/bench_tools.h
   hugo/trunk/benchmark/bfs-bench.cc
   hugo/trunk/benchmark/hcube.cc
   hugo/trunk/lemon/time_measure.h
   hugo/trunk/test/time_measure_test.cc

Log:
- 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


Modified: hugo/trunk/benchmark/bench_tools.h
==============================================================================
--- hugo/trunk/benchmark/bench_tools.h	(original)
+++ hugo/trunk/benchmark/bench_tools.h	Wed Sep 28 10:14:39 2005
@@ -83,15 +83,15 @@
 inline void PrintTime(char *ID,lemon::Timer &T) 
 {
   lemon::TimeStamp S(T);
-  std::cout << ID << ' ' << S.getUserTime() << ' '
-	    << S.getSystemTime() << ' ' << S.getRealTime() << std::endl;
+  std::cout << ID << ' ' << S.userTime() << ' '
+	    << S.systemTime() << ' ' << S.realTime() << std::endl;
 }
 
 
 
 ///
 template<class Graph>
-void addHiperCube(Graph &G,int dim,std::vector<typename Graph::Node> &nodes)
+void addHyperCube(Graph &G,int dim,std::vector<typename Graph::Node> &nodes)
 {
   GRAPH_TYPEDEF_FACTORY(Graph);
   
@@ -107,7 +107,7 @@
 
 ///
 template<class Graph>
-void addBiDirHiperCube(Graph &G,int dim,std::vector<typename Graph::Node>&nodes)
+void addBiDirHyperCube(Graph &G,int dim,std::vector<typename Graph::Node>&nodes)
 {
   GRAPH_TYPEDEF_FACTORY(Graph);
   

Modified: hugo/trunk/benchmark/bfs-bench.cc
==============================================================================
--- hugo/trunk/benchmark/bfs-bench.cc	(original)
+++ hugo/trunk/benchmark/bfs-bench.cc	Wed Sep 28 10:14:39 2005
@@ -117,7 +117,7 @@
 
   T.reset();
   vector<Node> nodes;
-  addBiDirHiperCube(G,dim,nodes);
+  addBiDirHyperCube(G,dim,nodes);
 
   PrintTime("GENGRAPH",T);
 

Modified: hugo/trunk/benchmark/hcube.cc
==============================================================================
--- hugo/trunk/benchmark/hcube.cc	(original)
+++ hugo/trunk/benchmark/hcube.cc	Wed Sep 28 10:14:39 2005
@@ -55,7 +55,7 @@
 
   T.reset();
   vector<Node> nodes;
-  addBiDirHiperCube(G,dim,nodes);
+  addBiDirHyperCube(G,dim,nodes);
 
   PrintTime("GENGRAPH",T);
 

Modified: hugo/trunk/lemon/time_measure.h
==============================================================================
--- hugo/trunk/lemon/time_measure.h	(original)
+++ hugo/trunk/lemon/time_measure.h	Wed Sep 28 10:14:39 2005
@@ -50,19 +50,32 @@
 
   class TimeStamp
   {
-    tms ts;
+    struct rtms 
+    {
+      double tms_utime;
+      double tms_stime;
+      double tms_cutime;
+      double tms_cstime;
+      rtms() {}
+      rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
+		     tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
+    };
+    rtms ts;
     double real_time;
   
+    rtms &getTms() {return ts;}
+    const rtms &getTms() const {return ts;}
+
   public:
 
-    tms &getTms() {return ts;}
-    const tms &getTms() const {return ts;}
     ///Read the current time values of the process
     void stamp()
     {
       timeval tv;
-      times(&ts);
+      tms _ts;
+      times(&_ts);
       gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
+      ts=_ts;
     }
   
     /// Constructor initializing with zero
@@ -103,6 +116,41 @@
       TimeStamp t(*this);
       return t-=b;
     }
+    ///\e
+
+    ///\bug operator * and / gives rounded values!
+    TimeStamp &operator*=(double b)
+    {
+      ts.tms_utime*=b;
+      ts.tms_stime*=b;
+      ts.tms_cutime*=b;
+      ts.tms_cstime*=b;
+      real_time*=b;
+      return *this;
+    }
+    ///\e
+    TimeStamp operator*(double b) const
+    {
+      TimeStamp t(*this);
+      return t*=b;
+    }
+    friend TimeStamp operator*(double b,const TimeStamp &t);
+    ///\e
+    TimeStamp &operator/=(double b)
+    {
+      ts.tms_utime/=b;
+      ts.tms_stime/=b;
+      ts.tms_cutime/=b;
+      ts.tms_cstime/=b;
+      real_time/=b;
+      return *this;
+    }
+    ///\e
+    TimeStamp operator/(double b) const
+    {
+      TimeStamp t(*this);
+      return t/=b;
+    }
     ///The time ellapsed since the last call of stamp()
     TimeStamp ellapsed() const
     {
@@ -113,29 +161,34 @@
     friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
   
     ///Gives back the user time of the process
-    double getUserTime() const
+    double userTime() const
     {
       return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
     }
     ///Gives back the system time of the process
-    double getSystemTime() const
+    double systemTime() const
     {
       return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
     }
     ///Gives back the user time of the process' children
-    double getCUserTime() const
+    double cUserTime() const
     {
       return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
     }
     ///Gives back the user time of the process' children
-    double getCSystemTime() const
+    double cSystemTime() const
     {
       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
     }
     ///Gives back the real time of the process
-    double getRealTime() const {return real_time;}
+    double realTime() const {return real_time;}
   };
 
+  TimeStamp operator*(double b,const TimeStamp &t) 
+  {
+    return t*b;
+  }
+  
   ///Class measuring the cpu time and real time usage of the process
 
   ///Class measuring the cpu time and real time usage of the process.
@@ -197,29 +250,29 @@
 
 
     ///Gives back the ellapsed user time of the process
-    double getUserTime() const
+    double userTime() const
     {
-      return operator TimeStamp().getUserTime();
+      return operator TimeStamp().userTime();
     }
     ///Gives back the ellapsed system time of the process
-    double getSystemTime() const
+    double systemTime() const
     {
-      return operator TimeStamp().getSystemTime();
+      return operator TimeStamp().systemTime();
     }
     ///Gives back the ellapsed user time of the process' children
-    double getCUserTime() const
+    double cUserTime() const
     {
-      return operator TimeStamp().getCUserTime();
+      return operator TimeStamp().cUserTime();
     }
     ///Gives back the ellapsed user time of the process' children
-    double getCSystemTime() const
+    double cSystemTime() const
     {
-      return operator TimeStamp().getCSystemTime();
+      return operator TimeStamp().cSystemTime();
     }
     ///Gives back the ellapsed real time of the process
-    double getRealTime() const
+    double realTime() const
     {
-      return operator TimeStamp().getRealTime();
+      return operator TimeStamp().realTime();
     }
 
   };
@@ -244,12 +297,48 @@
       "s, s: " << double(t.getTms().tms_stime)/cls <<
       "s, cu: " << double(t.getTms().tms_cutime)/cls <<
       "s, cs: " << double(t.getTms().tms_cstime)/cls <<
-      "s, real: " << t.getRealTime() << "s";
+      "s, real: " << t.realTime() << "s";
     return os;
   }
 
+  
+  ///Tool to measure the running time more exactly.
+  
+  ///This function calls \c f several times and returns the average
+  ///running time. The number of the executions will be choosen in such a way
+  ///that the full running time will be roughly between \c min_time
+  ///and <tt>2*min_time</tt>.
+  ///\param f the function object to be measured.
+  ///\param min_time the minimum total running time.
+  ///\retval num if it is not \c NULL, then *num will contain the actual
+  ///        number of execution of \c f.
+  ///\retval full_time if it is not \c NULL, then *full_time
+  ///        will contain the actual
+  ///        total running time.
+  ///\return The average running time of \c f.
+  
+  template<class F>
+  TimeStamp runningTimeTest(F &f,double min_time=10,int *num = NULL,
+			TimeStamp *full_time=NULL)
+  {
+    Timer t;
+    TimeStamp full;
+    int total=0;
+    for(int tn=1;tn < 1<<24; tn*=2) {
+      for(;total<tn;total++) f();
+      full=t;
+      if(full.realTime()>min_time) {
+	if(num) *num=total;
+	if(full_time) *full_time=full;
+      return full/total;
+      }
+    }
+    return TimeStamp();
+  }
+  
   /// @}  
 
+
 } //namespace lemon
 
 #endif //LEMON_TIME_MEASURE_H

Modified: hugo/trunk/test/time_measure_test.cc
==============================================================================
--- hugo/trunk/test/time_measure_test.cc	(original)
+++ hugo/trunk/test/time_measure_test.cc	Wed Sep 28 10:14:39 2005
@@ -23,14 +23,26 @@
 
 using namespace lemon;
 
+void f() 
+{
+  double d=0;
+  for(int i=0;i<10000;i++)
+    d+=0.1;
+}
+
 int main()
 {
   Timer T;
-  while(T.getRealTime()<1.0) ;
-  std::cout << T << '\n';
+  int n;
+  for(n=0;T.realTime()<1.0;n++) ;
+  std::cout << T << " (" << n << " time queries)\n";
   T.reset();
-  while(T.getRealTime()<2.0) ;
+  while(T.realTime()<2.0) ;
   std::cout << T << '\n';
+  TimeStamp full;
+  TimeStamp t=runningTimeTest(f,1,&n,&full);
+  std::cout << t << " (" << n << " tests)\n";
+  std::cout << "Total: " << full << "\n";
   
   return 0;
 }



More information about the Lemon-commits mailing list