[73] | 1 | // -*- c++ -*- |
---|
| 2 | #ifndef TIME_MEASURE_H |
---|
| 3 | #define TIME_MEASURE_H |
---|
| 4 | |
---|
| 5 | #include <sys/time.h> |
---|
[117] | 6 | #include <sys/times.h> |
---|
| 7 | #include <fstream> |
---|
| 8 | #include <iostream> |
---|
[73] | 9 | |
---|
| 10 | double currTime() { |
---|
| 11 | timeval tv; |
---|
| 12 | //timezone tz; |
---|
| 13 | gettimeofday(&tv, 0); |
---|
| 14 | return double(tv.tv_sec)+double(tv.tv_usec)/1000000.0; |
---|
| 15 | } |
---|
| 16 | |
---|
[117] | 17 | class TimeStamp |
---|
| 18 | { |
---|
| 19 | tms ts; |
---|
[118] | 20 | double real_time; |
---|
| 21 | |
---|
[117] | 22 | public: |
---|
| 23 | |
---|
| 24 | tms &getTms() {return ts;} |
---|
| 25 | const tms &getTms() const {return ts;} |
---|
[118] | 26 | double getRealTime() const {return real_time;} |
---|
| 27 | void stamp() |
---|
| 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 | |
---|
[117] | 37 | TimeStamp(void *) { stamp();} |
---|
| 38 | |
---|
| 39 | TimeStamp &operator+=(const TimeStamp &b) |
---|
| 40 | { |
---|
| 41 | ts.tms_utime+=b.ts.tms_utime; |
---|
| 42 | ts.tms_stime+=b.ts.tms_stime; |
---|
| 43 | ts.tms_cutime+=b.ts.tms_cutime; |
---|
| 44 | ts.tms_cstime+=b.ts.tms_cstime; |
---|
[118] | 45 | real_time+=b.real_time; |
---|
[117] | 46 | return *this; |
---|
| 47 | } |
---|
| 48 | TimeStamp operator+(const TimeStamp &b) const |
---|
| 49 | { |
---|
| 50 | TimeStamp t(*this); |
---|
| 51 | return t+=b; |
---|
| 52 | } |
---|
| 53 | TimeStamp &operator-=(const TimeStamp &b) |
---|
| 54 | { |
---|
| 55 | ts.tms_utime-=b.ts.tms_utime; |
---|
| 56 | ts.tms_stime-=b.ts.tms_stime; |
---|
| 57 | ts.tms_cutime-=b.ts.tms_cutime; |
---|
| 58 | ts.tms_cstime-=b.ts.tms_cstime; |
---|
[118] | 59 | real_time-=b.real_time; |
---|
[117] | 60 | return *this; |
---|
| 61 | } |
---|
| 62 | TimeStamp operator-(const TimeStamp &b) const |
---|
| 63 | { |
---|
| 64 | TimeStamp t(*this); |
---|
| 65 | return t-=b; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | TimeStamp Ellapsed() const |
---|
| 69 | { |
---|
| 70 | TimeStamp t(NULL); |
---|
| 71 | return t-*this; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
---|
| 75 | |
---|
[118] | 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 | } |
---|
[117] | 96 | }; |
---|
| 97 | |
---|
| 98 | class Timer |
---|
| 99 | { |
---|
| 100 | TimeStamp start_time; |
---|
| 101 | |
---|
| 102 | public: |
---|
| 103 | void reset() {start_time.stamp();} |
---|
| 104 | Timer() {reset();} |
---|
| 105 | |
---|
| 106 | operator TimeStamp () |
---|
| 107 | { |
---|
| 108 | TimeStamp t; |
---|
| 109 | t.stamp(); |
---|
| 110 | return t-start_time; |
---|
| 111 | } |
---|
| 112 | }; |
---|
| 113 | |
---|
| 114 | inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
---|
| 115 | { |
---|
| 116 | long cls = sysconf(_SC_CLK_TCK); |
---|
[118] | 117 | os << "u: " << double(t.getTms().tms_utime)/cls << |
---|
[117] | 118 | "s, s: " << double(t.getTms().tms_stime)/cls << |
---|
| 119 | "s, cu: " << double(t.getTms().tms_cutime)/cls << |
---|
[118] | 120 | "s, cs: " << double(t.getTms().tms_cstime)/cls << |
---|
| 121 | "s, real: " << t.getRealTime() << "s"; |
---|
[117] | 122 | return os; |
---|
| 123 | } |
---|
| 124 | |
---|
[73] | 125 | #endif //TIME_MEASURE_H |
---|