COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/time_measure.h @ 323:58bc28afea63

Last change on this file since 323:58bc28afea63 was 323:58bc28afea63, checked in by marci, 20 years ago

gw, kiszedtem ami nem kell

File size: 2.9 KB
RevLine 
[73]1// -*- c++ -*-
[259]2#ifndef HUGO_TIME_MEASURE_H
3#define HUGO_TIME_MEASURE_H
[73]4
5#include <sys/time.h>
[117]6#include <sys/times.h>
7#include <fstream>
8#include <iostream>
[121]9#include <unistd.h>
[73]10
[323]11namespace hugo {
[73]12
[323]13//   double currTime() {
14//     timeval tv;
15//     //timezone tz;
16//     gettimeofday(&tv, 0);
17//     return double(tv.tv_sec)+double(tv.tv_usec)/1000000.0;
18//   }
19
20  class TimeStamp
21  {
22    tms ts;
23    double real_time;
[118]24 
[323]25  public:
[117]26
[323]27    tms &getTms() {return ts;}
28    const tms &getTms() const {return ts;}
29    double getRealTime() const {return real_time;}
30    void stamp()
31    {
32      timeval tv;
33      times(&ts);
34      gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
35    }
36 
37    TimeStamp()
38    { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
39 
40    TimeStamp(void *) { stamp();}
41 
42    TimeStamp &operator+=(const TimeStamp &b)
43    {
44      ts.tms_utime+=b.ts.tms_utime;
45      ts.tms_stime+=b.ts.tms_stime;
46      ts.tms_cutime+=b.ts.tms_cutime;
47      ts.tms_cstime+=b.ts.tms_cstime;
48      real_time+=b.real_time;
49      return *this;
50    }
51    TimeStamp operator+(const TimeStamp &b) const
52    {
53      TimeStamp t(*this);
54      return t+=b;
55    }
56    TimeStamp &operator-=(const TimeStamp &b)
57    {
58      ts.tms_utime-=b.ts.tms_utime;
59      ts.tms_stime-=b.ts.tms_stime;
60      ts.tms_cutime-=b.ts.tms_cutime;
61      ts.tms_cstime-=b.ts.tms_cstime;
62      real_time-=b.real_time;
63      return *this;
64    }
65    TimeStamp operator-(const TimeStamp &b) const
66    {
67      TimeStamp t(*this);
68      return t-=b;
69    }
70
71    TimeStamp Ellapsed() const
72    {
73      TimeStamp t(NULL);
74      return t-*this;
75    }
76 
77    friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
78 
79    double getUserTime() const
80    {
81      return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
82    }
83    double getSystemTime() const
84    {
85      return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
86    }
87    double getCUserTime() const
88    {
89      return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
90    }
91    double getCSystemTime() const
92    {
93      return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
94    }
95  };
96
97  class Timer
[118]98  {
[323]99    TimeStamp start_time;
100
101    void _reset() {start_time.stamp();}
[118]102 
[323]103  public:
104    Timer() {_reset();}
105
106    operator TimeStamp ()
107    {
108      TimeStamp t;
109      t.stamp();
110      return t-start_time;
111    }
112
113    TimeStamp reset()
114    {
115      TimeStamp t(start_time);
116      _reset();
117      return start_time-t;
118    }
119  };
120
121  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
[117]122  {
[323]123    long cls = sysconf(_SC_CLK_TCK);
124    os << "u: " << double(t.getTms().tms_utime)/cls <<
125      "s, s: " << double(t.getTms().tms_stime)/cls <<
126      "s, cu: " << double(t.getTms().tms_cutime)/cls <<
127      "s, cs: " << double(t.getTms().tms_cstime)/cls <<
128      "s, real: " << t.getRealTime() << "s";
129    return os;
[117]130  }
131
[323]132} //namespace hugo
[117]133
[259]134#endif //HUGO_TIME_MEASURE_H
Note: See TracBrowser for help on using the repository browser.