- Changes in doc (spell check).
- SmallGraph is a class instead of being a typedef. (For the sake of doxygen.)
2 * src/lemon/time_measure.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, 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
107 ///The time ellapsed since the last call of stamp()
108 TimeStamp ellapsed() const
114 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
116 ///Gives back the user time of the process
117 double getUserTime() const
119 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
121 ///Gives back the system time of the process
122 double getSystemTime() const
124 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
126 ///Gives back the user time of the process' children
127 double getCUserTime() const
129 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
131 ///Gives back the user time of the process' children
132 double getCSystemTime() const
134 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
136 ///Gives back the real time of the process
137 double getRealTime() const {return real_time;}
140 ///Class measuring the cpu time and real time usage of the process
142 ///Class measuring the cpu time and real time usage of the process.
143 ///It is quite easy-to-use, here is a short example.
145 ///#include<lemon/time_measure.h>
146 ///#include<iostream>
155 /// std::cout << T << '\n';
157 /// doSomethingElse();
158 /// std::cout << T << '\n';
165 ///\todo This shouldn't be Unix (Linux) specific.
167 ///\author Alpar Juttner
170 TimeStamp start_time;
172 void _reset() {start_time.stamp();}
175 ///Constructor. It starts with zero time counters
178 ///Computes the ellapsed time
180 ///This conversion computes the ellapsed time
181 ///since the construction of \c t or since
182 ///the last \c t.reset().
183 operator TimeStamp ()
190 ///Resets the time counters
193 TimeStamp t(start_time);
199 ///Prints the time counters
201 ///Prints the time counters in the following form:
203 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
205 /// where the values are the
206 /// \li \c u: user cpu time,
207 /// \li \c s: system cpu time,
208 /// \li \c cu: user cpu time of children,
209 /// \li \c cs: system cpu time of children,
210 /// \li \c real: real time.
211 /// \relates TimeStamp
212 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
214 long cls = sysconf(_SC_CLK_TCK);
215 os << "u: " << double(t.getTms().tms_utime)/cls <<
216 "s, s: " << double(t.getTms().tms_stime)/cls <<
217 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
218 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
219 "s, real: " << t.getRealTime() << "s";
227 #endif //LEMON_TIME_MEASURE_H