2 #ifndef HUGO_TIME_MEASURE_H
3 #define HUGO_TIME_MEASURE_H
7 ///\brief Tools for measuring cpu usage
10 #include <sys/times.h>
20 /// A class to store (cpu)time instances.
22 /// This class stores five time values.
25 /// - a system cpu time
26 /// - a user cpu time of children
27 /// - a system cpu time of children
29 /// TimeStamp's can be added to or substracted from each other and
30 /// they can be pushed to a stream.
39 tms &getTms() {return ts;}
40 const tms &getTms() const {return ts;}
41 ///Read the current time values of the process
46 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
49 /// Constructor initializing with zero
51 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
52 ///Constructor initializing with the current time values of the process
53 TimeStamp(void *) { stamp();}
56 TimeStamp &operator+=(const TimeStamp &b)
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;
66 TimeStamp operator+(const TimeStamp &b) const
72 TimeStamp &operator-=(const TimeStamp &b)
74 ts.tms_utime-=b.ts.tms_utime;
75 ts.tms_stime-=b.ts.tms_stime;
76 ts.tms_cutime-=b.ts.tms_cutime;
77 ts.tms_cstime-=b.ts.tms_cstime;
78 real_time-=b.real_time;
82 TimeStamp operator-(const TimeStamp &b) const
88 ///The time ellapsed since the last call of stamp()
89 TimeStamp ellapsed() const
95 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
97 ///Gives back the user time of the process
98 double getUserTime() const
100 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
102 ///Gives back the system time of the process
103 double getSystemTime() const
105 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
107 ///Gives back the user time of the process' children
108 double getCUserTime() const
110 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
112 ///Gives back the user time of the process' children
113 double getCSystemTime() const
115 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
117 ///Gives back the real time of the process
118 double getRealTime() const {return real_time;}
121 ///Class measuring the cpu time and real time usage of the process
124 TimeStamp start_time;
126 void _reset() {start_time.stamp();}
129 ///Constructor. It starts with zero time counters
132 ///Computes the ellapsed time
134 ///This conversion computes the ellapsed time
135 ///since the construction of \c t or since
136 ///the last \c t.reset().
137 operator TimeStamp ()
144 ///Resets the time counters
147 TimeStamp t(start_time);
153 ///Prints the time counters
155 ///Prints the time counters in the folloing form:
157 /// u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs
159 /// where the values are the
160 /// - a user cpu time,
161 /// - a system cpu time,
162 /// - a user cpu time of children,
163 /// - a system cpu time of children and
167 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
169 long cls = sysconf(_SC_CLK_TCK);
170 os << "u: " << double(t.getTms().tms_utime)/cls <<
171 "s, s: " << double(t.getTms().tms_stime)/cls <<
172 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
173 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
174 "s, real: " << t.getRealTime() << "s";
182 #endif //HUGO_TIME_MEASURE_H