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