alpar@906: /* -*- C++ -*- ladanyi@1435: * lemon/time_measure.h - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@1164: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@906: alpar@921: #ifndef LEMON_TIME_MEASURE_H alpar@921: #define LEMON_TIME_MEASURE_H alpar@428: klao@491: ///\ingroup misc alpar@428: ///\file alpar@428: ///\brief Tools for measuring cpu usage alpar@428: alpar@428: #include alpar@428: #include alpar@428: #include alpar@428: #include alpar@428: #include alpar@428: alpar@921: namespace lemon { alpar@428: alpar@428: /// \addtogroup misc alpar@428: /// @{ alpar@428: alpar@428: /// A class to store (cpu)time instances. alpar@428: alpar@428: /// This class stores five time values. alpar@428: /// - a real time alpar@428: /// - a user cpu time alpar@428: /// - a system cpu time alpar@428: /// - a user cpu time of children alpar@428: /// - a system cpu time of children alpar@428: /// alpar@428: /// TimeStamp's can be added to or substracted from each other and alpar@428: /// they can be pushed to a stream. alpar@458: /// alpar@458: /// In most cases, perhaps \ref Timer class is what you want to use instead. alpar@458: /// alpar@458: ///\author Alpar Juttner alpar@428: alpar@428: class TimeStamp alpar@428: { alpar@428: tms ts; alpar@428: double real_time; alpar@428: alpar@428: public: alpar@428: alpar@428: tms &getTms() {return ts;} alpar@428: const tms &getTms() const {return ts;} alpar@428: ///Read the current time values of the process alpar@428: void stamp() alpar@428: { alpar@428: timeval tv; alpar@428: times(&ts); alpar@428: gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6; alpar@428: } alpar@428: alpar@428: /// Constructor initializing with zero alpar@428: TimeStamp() alpar@428: { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;} alpar@428: ///Constructor initializing with the current time values of the process alpar@428: TimeStamp(void *) { stamp();} alpar@428: alpar@1005: ///\e alpar@428: TimeStamp &operator+=(const TimeStamp &b) alpar@428: { alpar@428: ts.tms_utime+=b.ts.tms_utime; alpar@428: ts.tms_stime+=b.ts.tms_stime; alpar@428: ts.tms_cutime+=b.ts.tms_cutime; alpar@428: ts.tms_cstime+=b.ts.tms_cstime; alpar@428: real_time+=b.real_time; alpar@428: return *this; alpar@428: } alpar@1005: ///\e alpar@428: TimeStamp operator+(const TimeStamp &b) const alpar@428: { alpar@428: TimeStamp t(*this); alpar@428: return t+=b; alpar@428: } alpar@1005: ///\e alpar@428: TimeStamp &operator-=(const TimeStamp &b) alpar@428: { alpar@428: ts.tms_utime-=b.ts.tms_utime; alpar@428: ts.tms_stime-=b.ts.tms_stime; alpar@428: ts.tms_cutime-=b.ts.tms_cutime; alpar@428: ts.tms_cstime-=b.ts.tms_cstime; alpar@428: real_time-=b.real_time; alpar@428: return *this; alpar@428: } alpar@1005: ///\e alpar@428: TimeStamp operator-(const TimeStamp &b) const alpar@428: { alpar@428: TimeStamp t(*this); alpar@428: return t-=b; alpar@428: } alpar@428: ///The time ellapsed since the last call of stamp() alpar@428: TimeStamp ellapsed() const alpar@428: { alpar@428: TimeStamp t(NULL); alpar@428: return t-*this; alpar@428: } alpar@428: alpar@428: friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); alpar@428: alpar@428: ///Gives back the user time of the process alpar@428: double getUserTime() const alpar@428: { alpar@428: return double(ts.tms_utime)/sysconf(_SC_CLK_TCK); alpar@428: } alpar@428: ///Gives back the system time of the process alpar@428: double getSystemTime() const alpar@428: { alpar@428: return double(ts.tms_stime)/sysconf(_SC_CLK_TCK); alpar@428: } alpar@428: ///Gives back the user time of the process' children alpar@428: double getCUserTime() const alpar@428: { alpar@428: return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK); alpar@428: } alpar@428: ///Gives back the user time of the process' children alpar@428: double getCSystemTime() const alpar@428: { alpar@428: return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK); alpar@428: } alpar@428: ///Gives back the real time of the process alpar@428: double getRealTime() const {return real_time;} alpar@428: }; alpar@428: alpar@428: ///Class measuring the cpu time and real time usage of the process alpar@458: alpar@458: ///Class measuring the cpu time and real time usage of the process. alpar@458: ///It is quite easy-to-use, here is a short example. alpar@458: ///\code alpar@921: ///#include alpar@696: ///#include alpar@814: /// alpar@458: ///int main() alpar@458: ///{ alpar@458: /// alpar@458: /// ... alpar@458: /// alpar@696: /// Timer T; alpar@458: /// doSomething(); alpar@696: /// std::cout << T << '\n'; alpar@458: /// T.reset(); alpar@458: /// doSomethingElse(); alpar@696: /// std::cout << T << '\n'; alpar@458: /// alpar@458: /// ... alpar@458: /// alpar@458: ///} alpar@458: ///\endcode alpar@458: /// alpar@458: ///\todo This shouldn't be Unix (Linux) specific. alpar@458: /// alpar@458: ///\author Alpar Juttner alpar@428: class Timer alpar@428: { alpar@428: TimeStamp start_time; alpar@428: alpar@428: void _reset() {start_time.stamp();} alpar@428: alpar@428: public: alpar@428: ///Constructor. It starts with zero time counters alpar@428: Timer() {_reset();} alpar@428: alpar@428: ///Computes the ellapsed time alpar@428: alpar@428: ///This conversion computes the ellapsed time alpar@428: ///since the construction of \c t or since alpar@428: ///the last \c t.reset(). alpar@1005: operator TimeStamp () const alpar@428: { alpar@428: TimeStamp t; alpar@428: t.stamp(); alpar@428: return t-start_time; alpar@428: } alpar@428: alpar@428: ///Resets the time counters alpar@1069: alpar@1069: ///Resets the time counters alpar@1069: /// alpar@1069: void reset() alpar@428: { alpar@428: _reset(); alpar@428: } alpar@1005: alpar@1005: alpar@1005: ///Gives back the ellapsed user time of the process alpar@1005: double getUserTime() const alpar@1005: { alpar@1005: return operator TimeStamp().getUserTime(); alpar@1005: } alpar@1005: ///Gives back the ellapsed system time of the process alpar@1005: double getSystemTime() const alpar@1005: { alpar@1005: return operator TimeStamp().getSystemTime(); alpar@1005: } alpar@1005: ///Gives back the ellapsed user time of the process' children alpar@1005: double getCUserTime() const alpar@1005: { alpar@1005: return operator TimeStamp().getCUserTime(); alpar@1005: } alpar@1005: ///Gives back the ellapsed user time of the process' children alpar@1005: double getCSystemTime() const alpar@1005: { alpar@1005: return operator TimeStamp().getCSystemTime(); alpar@1005: } alpar@1005: ///Gives back the ellapsed real time of the process alpar@1005: double getRealTime() const alpar@1005: { alpar@1005: return operator TimeStamp().getRealTime(); alpar@1005: } alpar@1005: alpar@428: }; alpar@428: alpar@428: ///Prints the time counters alpar@428: klao@492: ///Prints the time counters in the following form: alpar@428: /// alpar@440: /// u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs alpar@428: /// alpar@428: /// where the values are the alpar@440: /// \li \c u: user cpu time, alpar@440: /// \li \c s: system cpu time, alpar@440: /// \li \c cu: user cpu time of children, alpar@440: /// \li \c cs: system cpu time of children, alpar@440: /// \li \c real: real time. alpar@814: /// \relates TimeStamp alpar@428: inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) alpar@428: { alpar@428: long cls = sysconf(_SC_CLK_TCK); alpar@428: os << "u: " << double(t.getTms().tms_utime)/cls << alpar@428: "s, s: " << double(t.getTms().tms_stime)/cls << alpar@428: "s, cu: " << double(t.getTms().tms_cutime)/cls << alpar@428: "s, cs: " << double(t.getTms().tms_cstime)/cls << alpar@428: "s, real: " << t.getRealTime() << "s"; alpar@428: return os; alpar@428: } alpar@428: alpar@428: /// @} alpar@428: alpar@921: } //namespace lemon alpar@428: alpar@921: #endif //LEMON_TIME_MEASURE_H