Added a short tutorial on using graphs.
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.
32 /// In most cases, perhaps \ref Timer class is what you want to use instead.
34 ///\author Alpar Juttner
43 tms &getTms() {return ts;}
44 const tms &getTms() const {return ts;}
45 ///Read the current time values of the process
50 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
53 /// Constructor initializing with zero
55 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
56 ///Constructor initializing with the current time values of the process
57 TimeStamp(void *) { stamp();}
60 TimeStamp &operator+=(const TimeStamp &b)
62 ts.tms_utime+=b.ts.tms_utime;
63 ts.tms_stime+=b.ts.tms_stime;
64 ts.tms_cutime+=b.ts.tms_cutime;
65 ts.tms_cstime+=b.ts.tms_cstime;
66 real_time+=b.real_time;
70 TimeStamp operator+(const TimeStamp &b) const
76 TimeStamp &operator-=(const TimeStamp &b)
78 ts.tms_utime-=b.ts.tms_utime;
79 ts.tms_stime-=b.ts.tms_stime;
80 ts.tms_cutime-=b.ts.tms_cutime;
81 ts.tms_cstime-=b.ts.tms_cstime;
82 real_time-=b.real_time;
86 TimeStamp operator-(const TimeStamp &b) const
92 ///The time ellapsed since the last call of stamp()
93 TimeStamp ellapsed() const
99 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
101 ///Gives back the user time of the process
102 double getUserTime() const
104 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
106 ///Gives back the system time of the process
107 double getSystemTime() const
109 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
111 ///Gives back the user time of the process' children
112 double getCUserTime() const
114 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
116 ///Gives back the user time of the process' children
117 double getCSystemTime() const
119 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
121 ///Gives back the real time of the process
122 double getRealTime() const {return real_time;}
125 ///Class measuring the cpu time and real time usage of the process
127 ///Class measuring the cpu time and real time usage of the process.
128 ///It is quite easy-to-use, here is a short example.
139 /// doSomethingElse();
147 ///\todo This shouldn't be Unix (Linux) specific.
149 ///\author Alpar Juttner
152 TimeStamp start_time;
154 void _reset() {start_time.stamp();}
157 ///Constructor. It starts with zero time counters
160 ///Computes the ellapsed time
162 ///This conversion computes the ellapsed time
163 ///since the construction of \c t or since
164 ///the last \c t.reset().
165 operator TimeStamp ()
172 ///Resets the time counters
175 TimeStamp t(start_time);
181 ///Prints the time counters
183 ///Prints the time counters in the following form:
185 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
187 /// where the values are the
188 /// \li \c u: user cpu time,
189 /// \li \c s: system cpu time,
190 /// \li \c cu: user cpu time of children,
191 /// \li \c cs: system cpu time of children,
192 /// \li \c real: real time.
193 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
195 long cls = sysconf(_SC_CLK_TCK);
196 os << "u: " << double(t.getTms().tms_utime)/cls <<
197 "s, s: " << double(t.getTms().tms_stime)/cls <<
198 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
199 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
200 "s, real: " << t.getRealTime() << "s";
208 #endif //HUGO_TIME_MEASURE_H