# HG changeset patch # User alpar # Date 1127895279 0 # Node ID f1795dafe42c2c38a0a5628d0748e932b3aead47 # Parent 61ce46476787ebc78c8c645274617382971ae7b2 - 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 diff -r 61ce46476787 -r f1795dafe42c benchmark/bench_tools.h --- a/benchmark/bench_tools.h Fri Sep 16 09:57:02 2005 +0000 +++ b/benchmark/bench_tools.h Wed Sep 28 08:14:39 2005 +0000 @@ -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 -void addHiperCube(Graph &G,int dim,std::vector &nodes) +void addHyperCube(Graph &G,int dim,std::vector &nodes) { GRAPH_TYPEDEF_FACTORY(Graph); @@ -107,7 +107,7 @@ /// template -void addBiDirHiperCube(Graph &G,int dim,std::vector&nodes) +void addBiDirHyperCube(Graph &G,int dim,std::vector&nodes) { GRAPH_TYPEDEF_FACTORY(Graph); diff -r 61ce46476787 -r f1795dafe42c benchmark/bfs-bench.cc --- a/benchmark/bfs-bench.cc Fri Sep 16 09:57:02 2005 +0000 +++ b/benchmark/bfs-bench.cc Wed Sep 28 08:14:39 2005 +0000 @@ -117,7 +117,7 @@ T.reset(); vector nodes; - addBiDirHiperCube(G,dim,nodes); + addBiDirHyperCube(G,dim,nodes); PrintTime("GENGRAPH",T); diff -r 61ce46476787 -r f1795dafe42c benchmark/hcube.cc --- a/benchmark/hcube.cc Fri Sep 16 09:57:02 2005 +0000 +++ b/benchmark/hcube.cc Wed Sep 28 08:14:39 2005 +0000 @@ -55,7 +55,7 @@ T.reset(); vector nodes; - addBiDirHiperCube(G,dim,nodes); + addBiDirHyperCube(G,dim,nodes); PrintTime("GENGRAPH",T); diff -r 61ce46476787 -r f1795dafe42c lemon/time_measure.h --- a/lemon/time_measure.h Fri Sep 16 09:57:02 2005 +0000 +++ b/lemon/time_measure.h Wed Sep 28 08:14:39 2005 +0000 @@ -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 2*min_time. + ///\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 + 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(;totalmin_time) { + if(num) *num=total; + if(full_time) *full_time=full; + return full/total; + } + } + return TimeStamp(); + } + /// @} + } //namespace lemon #endif //LEMON_TIME_MEASURE_H diff -r 61ce46476787 -r f1795dafe42c test/time_measure_test.cc --- a/test/time_measure_test.cc Fri Sep 16 09:57:02 2005 +0000 +++ b/test/time_measure_test.cc Wed Sep 28 08:14:39 2005 +0000 @@ -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) ; + int n; + for(n=0;T.realTime()<1.0;n++) ; + std::cout << T << " (" << n << " time queries)\n"; + T.reset(); + while(T.realTime()<2.0) ; std::cout << T << '\n'; - T.reset(); - while(T.getRealTime()<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; }