1.1 --- a/src/hugo/time_measure.h Wed Sep 29 14:12:26 2004 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,227 +0,0 @@
1.4 -/* -*- C++ -*-
1.5 - * src/hugo/time_measure.h - Part of HUGOlib, a generic C++ optimization library
1.6 - *
1.7 - * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 - * (Egervary Combinatorial Optimization Research Group, EGRES).
1.9 - *
1.10 - * Permission to use, modify and distribute this software is granted
1.11 - * provided that this copyright notice appears in all copies. For
1.12 - * precise terms see the accompanying LICENSE file.
1.13 - *
1.14 - * This software is provided "AS IS" with no warranty of any kind,
1.15 - * express or implied, and with no claim as to its suitability for any
1.16 - * purpose.
1.17 - *
1.18 - */
1.19 -
1.20 -#ifndef HUGO_TIME_MEASURE_H
1.21 -#define HUGO_TIME_MEASURE_H
1.22 -
1.23 -///\ingroup misc
1.24 -///\file
1.25 -///\brief Tools for measuring cpu usage
1.26 -
1.27 -#include <sys/time.h>
1.28 -#include <sys/times.h>
1.29 -#include <fstream>
1.30 -#include <iostream>
1.31 -#include <unistd.h>
1.32 -
1.33 -namespace hugo {
1.34 -
1.35 - /// \addtogroup misc
1.36 - /// @{
1.37 -
1.38 - /// A class to store (cpu)time instances.
1.39 -
1.40 - /// This class stores five time values.
1.41 - /// - a real time
1.42 - /// - a user cpu time
1.43 - /// - a system cpu time
1.44 - /// - a user cpu time of children
1.45 - /// - a system cpu time of children
1.46 - ///
1.47 - /// TimeStamp's can be added to or substracted from each other and
1.48 - /// they can be pushed to a stream.
1.49 - ///
1.50 - /// In most cases, perhaps \ref Timer class is what you want to use instead.
1.51 - ///
1.52 - ///\author Alpar Juttner
1.53 -
1.54 - class TimeStamp
1.55 - {
1.56 - tms ts;
1.57 - double real_time;
1.58 -
1.59 - public:
1.60 -
1.61 - tms &getTms() {return ts;}
1.62 - const tms &getTms() const {return ts;}
1.63 - ///Read the current time values of the process
1.64 - void stamp()
1.65 - {
1.66 - timeval tv;
1.67 - times(&ts);
1.68 - gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
1.69 - }
1.70 -
1.71 - /// Constructor initializing with zero
1.72 - TimeStamp()
1.73 - { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
1.74 - ///Constructor initializing with the current time values of the process
1.75 - TimeStamp(void *) { stamp();}
1.76 -
1.77 - ///
1.78 - TimeStamp &operator+=(const TimeStamp &b)
1.79 - {
1.80 - ts.tms_utime+=b.ts.tms_utime;
1.81 - ts.tms_stime+=b.ts.tms_stime;
1.82 - ts.tms_cutime+=b.ts.tms_cutime;
1.83 - ts.tms_cstime+=b.ts.tms_cstime;
1.84 - real_time+=b.real_time;
1.85 - return *this;
1.86 - }
1.87 - ///
1.88 - TimeStamp operator+(const TimeStamp &b) const
1.89 - {
1.90 - TimeStamp t(*this);
1.91 - return t+=b;
1.92 - }
1.93 - ///
1.94 - TimeStamp &operator-=(const TimeStamp &b)
1.95 - {
1.96 - ts.tms_utime-=b.ts.tms_utime;
1.97 - ts.tms_stime-=b.ts.tms_stime;
1.98 - ts.tms_cutime-=b.ts.tms_cutime;
1.99 - ts.tms_cstime-=b.ts.tms_cstime;
1.100 - real_time-=b.real_time;
1.101 - return *this;
1.102 - }
1.103 - ///
1.104 - TimeStamp operator-(const TimeStamp &b) const
1.105 - {
1.106 - TimeStamp t(*this);
1.107 - return t-=b;
1.108 - }
1.109 -
1.110 - ///The time ellapsed since the last call of stamp()
1.111 - TimeStamp ellapsed() const
1.112 - {
1.113 - TimeStamp t(NULL);
1.114 - return t-*this;
1.115 - }
1.116 -
1.117 - friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
1.118 -
1.119 - ///Gives back the user time of the process
1.120 - double getUserTime() const
1.121 - {
1.122 - return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
1.123 - }
1.124 - ///Gives back the system time of the process
1.125 - double getSystemTime() const
1.126 - {
1.127 - return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
1.128 - }
1.129 - ///Gives back the user time of the process' children
1.130 - double getCUserTime() const
1.131 - {
1.132 - return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
1.133 - }
1.134 - ///Gives back the user time of the process' children
1.135 - double getCSystemTime() const
1.136 - {
1.137 - return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
1.138 - }
1.139 - ///Gives back the real time of the process
1.140 - double getRealTime() const {return real_time;}
1.141 - };
1.142 -
1.143 - ///Class measuring the cpu time and real time usage of the process
1.144 -
1.145 - ///Class measuring the cpu time and real time usage of the process.
1.146 - ///It is quite easy-to-use, here is a short example.
1.147 - ///\code
1.148 - ///#include<hugo/time_measure.h>
1.149 - ///#include<iostream>
1.150 - ///
1.151 - ///int main()
1.152 - ///{
1.153 - ///
1.154 - /// ...
1.155 - ///
1.156 - /// Timer T;
1.157 - /// doSomething();
1.158 - /// std::cout << T << '\n';
1.159 - /// T.reset();
1.160 - /// doSomethingElse();
1.161 - /// std::cout << T << '\n';
1.162 - ///
1.163 - /// ...
1.164 - ///
1.165 - ///}
1.166 - ///\endcode
1.167 - ///
1.168 - ///\todo This shouldn't be Unix (Linux) specific.
1.169 - ///
1.170 - ///\author Alpar Juttner
1.171 - class Timer
1.172 - {
1.173 - TimeStamp start_time;
1.174 -
1.175 - void _reset() {start_time.stamp();}
1.176 -
1.177 - public:
1.178 - ///Constructor. It starts with zero time counters
1.179 - Timer() {_reset();}
1.180 -
1.181 - ///Computes the ellapsed time
1.182 -
1.183 - ///This conversion computes the ellapsed time
1.184 - ///since the construction of \c t or since
1.185 - ///the last \c t.reset().
1.186 - operator TimeStamp ()
1.187 - {
1.188 - TimeStamp t;
1.189 - t.stamp();
1.190 - return t-start_time;
1.191 - }
1.192 -
1.193 - ///Resets the time counters
1.194 - TimeStamp reset()
1.195 - {
1.196 - TimeStamp t(start_time);
1.197 - _reset();
1.198 - return start_time-t;
1.199 - }
1.200 - };
1.201 -
1.202 - ///Prints the time counters
1.203 -
1.204 - ///Prints the time counters in the following form:
1.205 - ///
1.206 - /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
1.207 - ///
1.208 - /// where the values are the
1.209 - /// \li \c u: user cpu time,
1.210 - /// \li \c s: system cpu time,
1.211 - /// \li \c cu: user cpu time of children,
1.212 - /// \li \c cs: system cpu time of children,
1.213 - /// \li \c real: real time.
1.214 - /// \relates TimeStamp
1.215 - inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
1.216 - {
1.217 - long cls = sysconf(_SC_CLK_TCK);
1.218 - os << "u: " << double(t.getTms().tms_utime)/cls <<
1.219 - "s, s: " << double(t.getTms().tms_stime)/cls <<
1.220 - "s, cu: " << double(t.getTms().tms_cutime)/cls <<
1.221 - "s, cs: " << double(t.getTms().tms_cstime)/cls <<
1.222 - "s, real: " << t.getRealTime() << "s";
1.223 - return os;
1.224 - }
1.225 -
1.226 - /// @}
1.227 -
1.228 -} //namespace hugo
1.229 -
1.230 -#endif //HUGO_TIME_MEASURE_H