DataFormatError::what() also prints the name of the exception.
2 * src/lemon/time_measure.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_TIME_MEASURE_H
18 #define LEMON_TIME_MEASURE_H
22 ///\brief Tools for measuring cpu usage
25 #include <sys/times.h>
35 /// A class to store (cpu)time instances.
37 /// This class stores five time values.
40 /// - a system cpu time
41 /// - a user cpu time of children
42 /// - a system cpu time of children
44 /// TimeStamp's can be added to or substracted from each other and
45 /// they can be pushed to a stream.
47 /// In most cases, perhaps \ref Timer class is what you want to use instead.
49 ///\author Alpar Juttner
58 tms &getTms() {return ts;}
59 const tms &getTms() const {return ts;}
60 ///Read the current time values of the process
65 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
68 /// Constructor initializing with zero
70 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
71 ///Constructor initializing with the current time values of the process
72 TimeStamp(void *) { stamp();}
75 TimeStamp &operator+=(const TimeStamp &b)
77 ts.tms_utime+=b.ts.tms_utime;
78 ts.tms_stime+=b.ts.tms_stime;
79 ts.tms_cutime+=b.ts.tms_cutime;
80 ts.tms_cstime+=b.ts.tms_cstime;
81 real_time+=b.real_time;
85 TimeStamp operator+(const TimeStamp &b) const
91 TimeStamp &operator-=(const TimeStamp &b)
93 ts.tms_utime-=b.ts.tms_utime;
94 ts.tms_stime-=b.ts.tms_stime;
95 ts.tms_cutime-=b.ts.tms_cutime;
96 ts.tms_cstime-=b.ts.tms_cstime;
97 real_time-=b.real_time;
101 TimeStamp operator-(const TimeStamp &b) const
106 ///The time ellapsed since the last call of stamp()
107 TimeStamp ellapsed() const
113 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
115 ///Gives back the user time of the process
116 double getUserTime() const
118 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
120 ///Gives back the system time of the process
121 double getSystemTime() const
123 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
125 ///Gives back the user time of the process' children
126 double getCUserTime() const
128 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
130 ///Gives back the user time of the process' children
131 double getCSystemTime() const
133 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
135 ///Gives back the real time of the process
136 double getRealTime() const {return real_time;}
139 ///Class measuring the cpu time and real time usage of the process
141 ///Class measuring the cpu time and real time usage of the process.
142 ///It is quite easy-to-use, here is a short example.
144 ///#include<lemon/time_measure.h>
145 ///#include<iostream>
154 /// std::cout << T << '\n';
156 /// doSomethingElse();
157 /// std::cout << T << '\n';
164 ///\todo This shouldn't be Unix (Linux) specific.
166 ///\author Alpar Juttner
169 TimeStamp start_time;
171 void _reset() {start_time.stamp();}
174 ///Constructor. It starts with zero time counters
177 ///Computes the ellapsed time
179 ///This conversion computes the ellapsed time
180 ///since the construction of \c t or since
181 ///the last \c t.reset().
182 operator TimeStamp () const
189 ///Resets the time counters
191 ///Resets the time counters
199 ///Gives back the ellapsed user time of the process
200 double getUserTime() const
202 return operator TimeStamp().getUserTime();
204 ///Gives back the ellapsed system time of the process
205 double getSystemTime() const
207 return operator TimeStamp().getSystemTime();
209 ///Gives back the ellapsed user time of the process' children
210 double getCUserTime() const
212 return operator TimeStamp().getCUserTime();
214 ///Gives back the ellapsed user time of the process' children
215 double getCSystemTime() const
217 return operator TimeStamp().getCSystemTime();
219 ///Gives back the ellapsed real time of the process
220 double getRealTime() const
222 return operator TimeStamp().getRealTime();
227 ///Prints the time counters
229 ///Prints the time counters in the following form:
231 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
233 /// where the values are the
234 /// \li \c u: user cpu time,
235 /// \li \c s: system cpu time,
236 /// \li \c cu: user cpu time of children,
237 /// \li \c cs: system cpu time of children,
238 /// \li \c real: real time.
239 /// \relates TimeStamp
240 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
242 long cls = sysconf(_SC_CLK_TCK);
243 os << "u: " << double(t.getTms().tms_utime)/cls <<
244 "s, s: " << double(t.getTms().tms_stime)/cls <<
245 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
246 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
247 "s, real: " << t.getRealTime() << "s";
255 #endif //LEMON_TIME_MEASURE_H