src/lemon/time_measure.h
changeset 1435 8e85e6bbefdf
parent 1434 d8475431bbbb
child 1436 e0beb94d08bf
     1.1 --- a/src/lemon/time_measure.h	Sat May 21 21:04:57 2005 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,255 +0,0 @@
     1.4 -/* -*- C++ -*-
     1.5 - * src/lemon/time_measure.h - Part of LEMON, a generic C++ optimization library
     1.6 - *
     1.7 - * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 - * (Egervary Research Group on Combinatorial Optimization, 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 LEMON_TIME_MEASURE_H
    1.21 -#define LEMON_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 lemon {
    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 -    ///\e
    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 -    ///\e
    1.88 -    TimeStamp operator+(const TimeStamp &b) const
    1.89 -    {
    1.90 -      TimeStamp t(*this);
    1.91 -      return t+=b;
    1.92 -    }
    1.93 -    ///\e
    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 -    ///\e
   1.104 -    TimeStamp operator-(const TimeStamp &b) const
   1.105 -    {
   1.106 -      TimeStamp t(*this);
   1.107 -      return t-=b;
   1.108 -    }
   1.109 -    ///The time ellapsed since the last call of stamp()
   1.110 -    TimeStamp ellapsed() const
   1.111 -    {
   1.112 -      TimeStamp t(NULL);
   1.113 -      return t-*this;
   1.114 -    }
   1.115 -  
   1.116 -    friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
   1.117 -  
   1.118 -    ///Gives back the user time of the process
   1.119 -    double getUserTime() const
   1.120 -    {
   1.121 -      return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
   1.122 -    }
   1.123 -    ///Gives back the system time of the process
   1.124 -    double getSystemTime() const
   1.125 -    {
   1.126 -      return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
   1.127 -    }
   1.128 -    ///Gives back the user time of the process' children
   1.129 -    double getCUserTime() const
   1.130 -    {
   1.131 -      return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
   1.132 -    }
   1.133 -    ///Gives back the user time of the process' children
   1.134 -    double getCSystemTime() const
   1.135 -    {
   1.136 -      return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   1.137 -    }
   1.138 -    ///Gives back the real time of the process
   1.139 -    double getRealTime() const {return real_time;}
   1.140 -  };
   1.141 -
   1.142 -  ///Class measuring the cpu time and real time usage of the process
   1.143 -
   1.144 -  ///Class measuring the cpu time and real time usage of the process.
   1.145 -  ///It is quite easy-to-use, here is a short example.
   1.146 -  ///\code
   1.147 -  ///#include<lemon/time_measure.h>
   1.148 -  ///#include<iostream>
   1.149 -  ///
   1.150 -  ///int main()
   1.151 -  ///{
   1.152 -  ///
   1.153 -  ///  ...
   1.154 -  ///
   1.155 -  ///  Timer T;
   1.156 -  ///  doSomething();
   1.157 -  ///  std::cout << T << '\n';
   1.158 -  ///  T.reset();
   1.159 -  ///  doSomethingElse();
   1.160 -  ///  std::cout << T << '\n';
   1.161 -  ///
   1.162 -  ///  ...
   1.163 -  ///
   1.164 -  ///}
   1.165 -  ///\endcode
   1.166 -  ///
   1.167 -  ///\todo This shouldn't be Unix (Linux) specific.
   1.168 -  ///
   1.169 -  ///\author Alpar Juttner
   1.170 -  class Timer
   1.171 -  {
   1.172 -    TimeStamp start_time;
   1.173 -
   1.174 -    void _reset() {start_time.stamp();}
   1.175 -  
   1.176 -  public: 
   1.177 -    ///Constructor. It starts with zero time counters
   1.178 -    Timer() {_reset();}
   1.179 -
   1.180 -    ///Computes the ellapsed time
   1.181 -
   1.182 -    ///This conversion computes the ellapsed time
   1.183 -    ///since the construction of \c t or since
   1.184 -    ///the last \c t.reset().
   1.185 -    operator TimeStamp () const
   1.186 -    {
   1.187 -      TimeStamp t;
   1.188 -      t.stamp();
   1.189 -      return t-start_time;
   1.190 -    }
   1.191 -
   1.192 -    ///Resets the time counters
   1.193 -
   1.194 -    ///Resets the time counters
   1.195 -    ///
   1.196 -    void reset()
   1.197 -    {
   1.198 -      _reset();
   1.199 -    }
   1.200 -
   1.201 -
   1.202 -    ///Gives back the ellapsed user time of the process
   1.203 -    double getUserTime() const
   1.204 -    {
   1.205 -      return operator TimeStamp().getUserTime();
   1.206 -    }
   1.207 -    ///Gives back the ellapsed system time of the process
   1.208 -    double getSystemTime() const
   1.209 -    {
   1.210 -      return operator TimeStamp().getSystemTime();
   1.211 -    }
   1.212 -    ///Gives back the ellapsed user time of the process' children
   1.213 -    double getCUserTime() const
   1.214 -    {
   1.215 -      return operator TimeStamp().getCUserTime();
   1.216 -    }
   1.217 -    ///Gives back the ellapsed user time of the process' children
   1.218 -    double getCSystemTime() const
   1.219 -    {
   1.220 -      return operator TimeStamp().getCSystemTime();
   1.221 -    }
   1.222 -    ///Gives back the ellapsed real time of the process
   1.223 -    double getRealTime() const
   1.224 -    {
   1.225 -      return operator TimeStamp().getRealTime();
   1.226 -    }
   1.227 -
   1.228 -  };
   1.229 -
   1.230 -  ///Prints the time counters
   1.231 -
   1.232 -  ///Prints the time counters in the following form:
   1.233 -  ///
   1.234 -  /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
   1.235 -  ///
   1.236 -  /// where the values are the
   1.237 -  /// \li \c u: user cpu time,
   1.238 -  /// \li \c s: system cpu time,
   1.239 -  /// \li \c cu: user cpu time of children,
   1.240 -  /// \li \c cs: system cpu time of children,
   1.241 -  /// \li \c real: real time.
   1.242 -  /// \relates TimeStamp
   1.243 -  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
   1.244 -  {
   1.245 -    long cls = sysconf(_SC_CLK_TCK);
   1.246 -    os << "u: " << double(t.getTms().tms_utime)/cls <<
   1.247 -      "s, s: " << double(t.getTms().tms_stime)/cls <<
   1.248 -      "s, cu: " << double(t.getTms().tms_cutime)/cls <<
   1.249 -      "s, cs: " << double(t.getTms().tms_cstime)/cls <<
   1.250 -      "s, real: " << t.getRealTime() << "s";
   1.251 -    return os;
   1.252 -  }
   1.253 -
   1.254 -  /// @}  
   1.255 -
   1.256 -} //namespace lemon
   1.257 -
   1.258 -#endif //LEMON_TIME_MEASURE_H