[906] | 1 | /* -*- C++ -*- |
---|
[921] | 2 | * src/lemon/time_measure.h - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
| 4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
| 6 | * |
---|
| 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. |
---|
| 10 | * |
---|
| 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 |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
[921] | 17 | #ifndef LEMON_TIME_MEASURE_H |
---|
| 18 | #define LEMON_TIME_MEASURE_H |
---|
[428] | 19 | |
---|
[491] | 20 | ///\ingroup misc |
---|
[428] | 21 | ///\file |
---|
| 22 | ///\brief Tools for measuring cpu usage |
---|
| 23 | |
---|
| 24 | #include <sys/time.h> |
---|
| 25 | #include <sys/times.h> |
---|
| 26 | #include <fstream> |
---|
| 27 | #include <iostream> |
---|
| 28 | #include <unistd.h> |
---|
| 29 | |
---|
[921] | 30 | namespace lemon { |
---|
[428] | 31 | |
---|
| 32 | /// \addtogroup misc |
---|
| 33 | /// @{ |
---|
| 34 | |
---|
| 35 | /// A class to store (cpu)time instances. |
---|
| 36 | |
---|
| 37 | /// This class stores five time values. |
---|
| 38 | /// - a real time |
---|
| 39 | /// - a user cpu time |
---|
| 40 | /// - a system cpu time |
---|
| 41 | /// - a user cpu time of children |
---|
| 42 | /// - a system cpu time of children |
---|
| 43 | /// |
---|
| 44 | /// TimeStamp's can be added to or substracted from each other and |
---|
| 45 | /// they can be pushed to a stream. |
---|
[458] | 46 | /// |
---|
| 47 | /// In most cases, perhaps \ref Timer class is what you want to use instead. |
---|
| 48 | /// |
---|
| 49 | ///\author Alpar Juttner |
---|
[428] | 50 | |
---|
| 51 | class TimeStamp |
---|
| 52 | { |
---|
| 53 | tms ts; |
---|
| 54 | double real_time; |
---|
| 55 | |
---|
| 56 | public: |
---|
| 57 | |
---|
| 58 | tms &getTms() {return ts;} |
---|
| 59 | const tms &getTms() const {return ts;} |
---|
| 60 | ///Read the current time values of the process |
---|
| 61 | void stamp() |
---|
| 62 | { |
---|
| 63 | timeval tv; |
---|
| 64 | times(&ts); |
---|
| 65 | gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /// Constructor initializing with zero |
---|
| 69 | TimeStamp() |
---|
| 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();} |
---|
| 73 | |
---|
| 74 | /// |
---|
| 75 | TimeStamp &operator+=(const TimeStamp &b) |
---|
| 76 | { |
---|
| 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; |
---|
| 82 | return *this; |
---|
| 83 | } |
---|
| 84 | /// |
---|
| 85 | TimeStamp operator+(const TimeStamp &b) const |
---|
| 86 | { |
---|
| 87 | TimeStamp t(*this); |
---|
| 88 | return t+=b; |
---|
| 89 | } |
---|
| 90 | /// |
---|
| 91 | TimeStamp &operator-=(const TimeStamp &b) |
---|
| 92 | { |
---|
| 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; |
---|
| 98 | return *this; |
---|
| 99 | } |
---|
| 100 | /// |
---|
| 101 | TimeStamp operator-(const TimeStamp &b) const |
---|
| 102 | { |
---|
| 103 | TimeStamp t(*this); |
---|
| 104 | return t-=b; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | ///The time ellapsed since the last call of stamp() |
---|
| 108 | TimeStamp ellapsed() const |
---|
| 109 | { |
---|
| 110 | TimeStamp t(NULL); |
---|
| 111 | return t-*this; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
---|
| 115 | |
---|
| 116 | ///Gives back the user time of the process |
---|
| 117 | double getUserTime() const |
---|
| 118 | { |
---|
| 119 | return double(ts.tms_utime)/sysconf(_SC_CLK_TCK); |
---|
| 120 | } |
---|
| 121 | ///Gives back the system time of the process |
---|
| 122 | double getSystemTime() const |
---|
| 123 | { |
---|
| 124 | return double(ts.tms_stime)/sysconf(_SC_CLK_TCK); |
---|
| 125 | } |
---|
| 126 | ///Gives back the user time of the process' children |
---|
| 127 | double getCUserTime() const |
---|
| 128 | { |
---|
| 129 | return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK); |
---|
| 130 | } |
---|
| 131 | ///Gives back the user time of the process' children |
---|
| 132 | double getCSystemTime() const |
---|
| 133 | { |
---|
| 134 | return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK); |
---|
| 135 | } |
---|
| 136 | ///Gives back the real time of the process |
---|
| 137 | double getRealTime() const {return real_time;} |
---|
| 138 | }; |
---|
| 139 | |
---|
| 140 | ///Class measuring the cpu time and real time usage of the process |
---|
[458] | 141 | |
---|
| 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. |
---|
| 144 | ///\code |
---|
[921] | 145 | ///#include<lemon/time_measure.h> |
---|
[696] | 146 | ///#include<iostream> |
---|
[814] | 147 | /// |
---|
[458] | 148 | ///int main() |
---|
| 149 | ///{ |
---|
| 150 | /// |
---|
| 151 | /// ... |
---|
| 152 | /// |
---|
[696] | 153 | /// Timer T; |
---|
[458] | 154 | /// doSomething(); |
---|
[696] | 155 | /// std::cout << T << '\n'; |
---|
[458] | 156 | /// T.reset(); |
---|
| 157 | /// doSomethingElse(); |
---|
[696] | 158 | /// std::cout << T << '\n'; |
---|
[458] | 159 | /// |
---|
| 160 | /// ... |
---|
| 161 | /// |
---|
| 162 | ///} |
---|
| 163 | ///\endcode |
---|
| 164 | /// |
---|
| 165 | ///\todo This shouldn't be Unix (Linux) specific. |
---|
| 166 | /// |
---|
| 167 | ///\author Alpar Juttner |
---|
[428] | 168 | class Timer |
---|
| 169 | { |
---|
| 170 | TimeStamp start_time; |
---|
| 171 | |
---|
| 172 | void _reset() {start_time.stamp();} |
---|
| 173 | |
---|
| 174 | public: |
---|
| 175 | ///Constructor. It starts with zero time counters |
---|
| 176 | Timer() {_reset();} |
---|
| 177 | |
---|
| 178 | ///Computes the ellapsed time |
---|
| 179 | |
---|
| 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 () |
---|
| 184 | { |
---|
| 185 | TimeStamp t; |
---|
| 186 | t.stamp(); |
---|
| 187 | return t-start_time; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | ///Resets the time counters |
---|
| 191 | TimeStamp reset() |
---|
| 192 | { |
---|
| 193 | TimeStamp t(start_time); |
---|
| 194 | _reset(); |
---|
| 195 | return start_time-t; |
---|
| 196 | } |
---|
| 197 | }; |
---|
| 198 | |
---|
| 199 | ///Prints the time counters |
---|
| 200 | |
---|
[492] | 201 | ///Prints the time counters in the following form: |
---|
[428] | 202 | /// |
---|
[440] | 203 | /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt> |
---|
[428] | 204 | /// |
---|
| 205 | /// where the values are the |
---|
[440] | 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. |
---|
[814] | 211 | /// \relates TimeStamp |
---|
[428] | 212 | inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
---|
| 213 | { |
---|
| 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"; |
---|
| 220 | return os; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | /// @} |
---|
| 224 | |
---|
[921] | 225 | } //namespace lemon |
---|
[428] | 226 | |
---|
[921] | 227 | #endif //LEMON_TIME_MEASURE_H |
---|