src/work/marci/time_measure.h
changeset 119 9b3345f9d8ed
parent 117 67253d52b284
child 121 9d5a99b282c0
equal deleted inserted replaced
1:1691a141daea 2:cc6a63024306
    15 }
    15 }
    16 
    16 
    17 class TimeStamp
    17 class TimeStamp
    18 {
    18 {
    19   tms ts;
    19   tms ts;
       
    20   double real_time;
       
    21   
    20 public:
    22 public:
    21 
    23 
    22   tms &getTms() {return ts;}
    24   tms &getTms() {return ts;}
    23   const tms &getTms() const {return ts;}
    25   const tms &getTms() const {return ts;}
    24 
    26   double getRealTime() const {return real_time;}
    25   void stamp() {times(&ts);}
    27   void stamp()
    26   TimeStamp() { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0;}
    28   {
       
    29     timeval tv;
       
    30     times(&ts);
       
    31     gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
       
    32   }
       
    33   
       
    34   TimeStamp()
       
    35   { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
       
    36   
    27   TimeStamp(void *) { stamp();}
    37   TimeStamp(void *) { stamp();}
    28   
    38   
    29   TimeStamp &operator+=(const TimeStamp &b)
    39   TimeStamp &operator+=(const TimeStamp &b)
    30   {
    40   {
    31     ts.tms_utime+=b.ts.tms_utime;
    41     ts.tms_utime+=b.ts.tms_utime;
    32     ts.tms_stime+=b.ts.tms_stime;
    42     ts.tms_stime+=b.ts.tms_stime;
    33     ts.tms_cutime+=b.ts.tms_cutime;
    43     ts.tms_cutime+=b.ts.tms_cutime;
    34     ts.tms_cstime+=b.ts.tms_cstime;
    44     ts.tms_cstime+=b.ts.tms_cstime;
       
    45     real_time+=b.real_time;
    35     return *this;
    46     return *this;
    36   }
    47   }
    37   TimeStamp operator+(const TimeStamp &b) const
    48   TimeStamp operator+(const TimeStamp &b) const
    38   {
    49   {
    39     TimeStamp t(*this);
    50     TimeStamp t(*this);
    43   {
    54   {
    44     ts.tms_utime-=b.ts.tms_utime;
    55     ts.tms_utime-=b.ts.tms_utime;
    45     ts.tms_stime-=b.ts.tms_stime;
    56     ts.tms_stime-=b.ts.tms_stime;
    46     ts.tms_cutime-=b.ts.tms_cutime;
    57     ts.tms_cutime-=b.ts.tms_cutime;
    47     ts.tms_cstime-=b.ts.tms_cstime;
    58     ts.tms_cstime-=b.ts.tms_cstime;
       
    59     real_time-=b.real_time;
    48     return *this;
    60     return *this;
    49   }
    61   }
    50   TimeStamp operator-(const TimeStamp &b) const
    62   TimeStamp operator-(const TimeStamp &b) const
    51   {
    63   {
    52     TimeStamp t(*this);
    64     TimeStamp t(*this);
    59     return t-*this;
    71     return t-*this;
    60   }
    72   }
    61   
    73   
    62   friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
    74   friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
    63   
    75   
       
    76   double getUserTime() const
       
    77   {
       
    78     long cls = sysconf(_SC_CLK_TCK);
       
    79     return double(ts.tms_utime)/cls;
       
    80   }
       
    81   double getSystemTime() const
       
    82   {
       
    83     long cls = sysconf(_SC_CLK_TCK);
       
    84     return double(ts.tms_stime)/cls;
       
    85   }
       
    86   double getCUserTime() const
       
    87   {
       
    88     long cls = sysconf(_SC_CLK_TCK);
       
    89     return double(ts.tms_cutime)/cls;
       
    90   }
       
    91   double getCSystemTime() const
       
    92   {
       
    93     long cls = sysconf(_SC_CLK_TCK);
       
    94     return double(ts.tms_cstime)/cls;
       
    95   }
    64 };
    96 };
    65 
    97 
    66 class Timer
    98 class Timer
    67 {
    99 {
    68   TimeStamp start_time;
   100   TimeStamp start_time;
    80 };
   112 };
    81 
   113 
    82 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
   114 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
    83 {
   115 {
    84   long cls = sysconf(_SC_CLK_TCK);
   116   long cls = sysconf(_SC_CLK_TCK);
    85   os << "[ u: " << double(t.getTms().tms_utime)/cls <<
   117   os << "u: " << double(t.getTms().tms_utime)/cls <<
    86     "s, s: " << double(t.getTms().tms_stime)/cls <<
   118     "s, s: " << double(t.getTms().tms_stime)/cls <<
    87     "s, cu: " << double(t.getTms().tms_cutime)/cls <<
   119     "s, cu: " << double(t.getTms().tms_cutime)/cls <<
    88     "s, cs: " << double(t.getTms().tms_cstime)/cls << "s ]";
   120     "s, cs: " << double(t.getTms().tms_cstime)/cls <<
       
   121     "s, real: " << t.getRealTime() << "s";
    89   return os;
   122   return os;
    90 }
   123 }
    91 
   124 
    92 #endif //TIME_MEASURE_H
   125 #endif //TIME_MEASURE_H