Timer class for measuring user/system time added.
authoralpar
Sun, 22 Feb 2004 15:17:58 +0000
changeset 11767253d52b284
parent 116 a987c6013ea0
child 118 38e16c594a4f
Timer class for measuring user/system time added.
src/work/alpar/f_ed_ka_demo.cc
src/work/marci/time_measure.h
     1.1 --- a/src/work/alpar/f_ed_ka_demo.cc	Sun Feb 22 15:16:54 2004 +0000
     1.2 +++ b/src/work/alpar/f_ed_ka_demo.cc	Sun Feb 22 15:17:58 2004 +0000
     1.3 @@ -31,8 +31,12 @@
     1.4    
     1.5    int ret;
     1.6    double pre_time=currTime();
     1.7 +  Timer ts;
     1.8 +  
     1.9    ret = maxFlow(G,flow,cap,s,t);
    1.10    double post_time=currTime();
    1.11 +  std::cout << "ellapsed time:" << ts << std::endl;
    1.12 +
    1.13    //std::cout << "maximum flow: "<< std::endl;
    1.14    //for(EachEdgeIt e=G.first<EachEdgeIt>(); e.valid(); ++e) { 
    1.15    //  std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
     2.1 --- a/src/work/marci/time_measure.h	Sun Feb 22 15:16:54 2004 +0000
     2.2 +++ b/src/work/marci/time_measure.h	Sun Feb 22 15:17:58 2004 +0000
     2.3 @@ -3,6 +3,9 @@
     2.4  #define TIME_MEASURE_H
     2.5  
     2.6  #include <sys/time.h>
     2.7 +#include <sys/times.h>
     2.8 +#include <fstream>
     2.9 +#include <iostream>
    2.10  
    2.11  double currTime() {
    2.12    timeval tv;
    2.13 @@ -11,4 +14,79 @@
    2.14    return double(tv.tv_sec)+double(tv.tv_usec)/1000000.0;
    2.15  }
    2.16  
    2.17 +class TimeStamp
    2.18 +{
    2.19 +  tms ts;
    2.20 +public:
    2.21 +
    2.22 +  tms &getTms() {return ts;}
    2.23 +  const tms &getTms() const {return ts;}
    2.24 +
    2.25 +  void stamp() {times(&ts);}
    2.26 +  TimeStamp() { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0;}
    2.27 +  TimeStamp(void *) { stamp();}
    2.28 +  
    2.29 +  TimeStamp &operator+=(const TimeStamp &b)
    2.30 +  {
    2.31 +    ts.tms_utime+=b.ts.tms_utime;
    2.32 +    ts.tms_stime+=b.ts.tms_stime;
    2.33 +    ts.tms_cutime+=b.ts.tms_cutime;
    2.34 +    ts.tms_cstime+=b.ts.tms_cstime;
    2.35 +    return *this;
    2.36 +  }
    2.37 +  TimeStamp operator+(const TimeStamp &b) const
    2.38 +  {
    2.39 +    TimeStamp t(*this);
    2.40 +    return t+=b;
    2.41 +  }
    2.42 +  TimeStamp &operator-=(const TimeStamp &b)
    2.43 +  {
    2.44 +    ts.tms_utime-=b.ts.tms_utime;
    2.45 +    ts.tms_stime-=b.ts.tms_stime;
    2.46 +    ts.tms_cutime-=b.ts.tms_cutime;
    2.47 +    ts.tms_cstime-=b.ts.tms_cstime;
    2.48 +    return *this;
    2.49 +  }
    2.50 +  TimeStamp operator-(const TimeStamp &b) const
    2.51 +  {
    2.52 +    TimeStamp t(*this);
    2.53 +    return t-=b;
    2.54 +  }
    2.55 +
    2.56 +  TimeStamp Ellapsed() const
    2.57 +  {
    2.58 +    TimeStamp t(NULL);
    2.59 +    return t-*this;
    2.60 +  }
    2.61 +  
    2.62 +  friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
    2.63 +  
    2.64 +};
    2.65 +
    2.66 +class Timer
    2.67 +{
    2.68 +  TimeStamp start_time;
    2.69 +
    2.70 +public: 
    2.71 +  void reset() {start_time.stamp();}
    2.72 +  Timer() {reset();}
    2.73 +
    2.74 +  operator TimeStamp ()
    2.75 +  {
    2.76 +    TimeStamp t;
    2.77 +    t.stamp();
    2.78 +    return t-start_time;
    2.79 +  }  
    2.80 +};
    2.81 +
    2.82 +inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
    2.83 +{
    2.84 +  long cls = sysconf(_SC_CLK_TCK);
    2.85 +  os << "[ u: " << double(t.getTms().tms_utime)/cls <<
    2.86 +    "s, s: " << double(t.getTms().tms_stime)/cls <<
    2.87 +    "s, cu: " << double(t.getTms().tms_cutime)/cls <<
    2.88 +    "s, cs: " << double(t.getTms().tms_cstime)/cls << "s ]";
    2.89 +  return os;
    2.90 +}
    2.91 +
    2.92  #endif //TIME_MEASURE_H