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