src/lemon/time_measure.h
author deba
Tue, 19 Apr 2005 13:33:44 +0000
changeset 1374 bcfa3980b432
parent 1164 80bb73097736
permissions -rw-r--r--
Call the default constructor of the ObserverBase.
     1 /* -*- C++ -*-
     2  * src/lemon/time_measure.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, 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 
    17 #ifndef LEMON_TIME_MEASURE_H
    18 #define LEMON_TIME_MEASURE_H
    19 
    20 ///\ingroup misc
    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 
    30 namespace lemon {
    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.
    46   ///
    47   /// In most cases, perhaps \ref Timer class is what you want to use instead.
    48   ///
    49   ///\author Alpar Juttner
    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     ///\e
    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     ///\e
    85     TimeStamp operator+(const TimeStamp &b) const
    86     {
    87       TimeStamp t(*this);
    88       return t+=b;
    89     }
    90     ///\e
    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     ///\e
   101     TimeStamp operator-(const TimeStamp &b) const
   102     {
   103       TimeStamp t(*this);
   104       return t-=b;
   105     }
   106     ///The time ellapsed since the last call of stamp()
   107     TimeStamp ellapsed() const
   108     {
   109       TimeStamp t(NULL);
   110       return t-*this;
   111     }
   112   
   113     friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
   114   
   115     ///Gives back the user time of the process
   116     double getUserTime() const
   117     {
   118       return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
   119     }
   120     ///Gives back the system time of the process
   121     double getSystemTime() const
   122     {
   123       return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
   124     }
   125     ///Gives back the user time of the process' children
   126     double getCUserTime() const
   127     {
   128       return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
   129     }
   130     ///Gives back the user time of the process' children
   131     double getCSystemTime() const
   132     {
   133       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   134     }
   135     ///Gives back the real time of the process
   136     double getRealTime() const {return real_time;}
   137   };
   138 
   139   ///Class measuring the cpu time and real time usage of the process
   140 
   141   ///Class measuring the cpu time and real time usage of the process.
   142   ///It is quite easy-to-use, here is a short example.
   143   ///\code
   144   ///#include<lemon/time_measure.h>
   145   ///#include<iostream>
   146   ///
   147   ///int main()
   148   ///{
   149   ///
   150   ///  ...
   151   ///
   152   ///  Timer T;
   153   ///  doSomething();
   154   ///  std::cout << T << '\n';
   155   ///  T.reset();
   156   ///  doSomethingElse();
   157   ///  std::cout << T << '\n';
   158   ///
   159   ///  ...
   160   ///
   161   ///}
   162   ///\endcode
   163   ///
   164   ///\todo This shouldn't be Unix (Linux) specific.
   165   ///
   166   ///\author Alpar Juttner
   167   class Timer
   168   {
   169     TimeStamp start_time;
   170 
   171     void _reset() {start_time.stamp();}
   172   
   173   public: 
   174     ///Constructor. It starts with zero time counters
   175     Timer() {_reset();}
   176 
   177     ///Computes the ellapsed time
   178 
   179     ///This conversion computes the ellapsed time
   180     ///since the construction of \c t or since
   181     ///the last \c t.reset().
   182     operator TimeStamp () const
   183     {
   184       TimeStamp t;
   185       t.stamp();
   186       return t-start_time;
   187     }
   188 
   189     ///Resets the time counters
   190 
   191     ///Resets the time counters
   192     ///
   193     void reset()
   194     {
   195       _reset();
   196     }
   197 
   198 
   199     ///Gives back the ellapsed user time of the process
   200     double getUserTime() const
   201     {
   202       return operator TimeStamp().getUserTime();
   203     }
   204     ///Gives back the ellapsed system time of the process
   205     double getSystemTime() const
   206     {
   207       return operator TimeStamp().getSystemTime();
   208     }
   209     ///Gives back the ellapsed user time of the process' children
   210     double getCUserTime() const
   211     {
   212       return operator TimeStamp().getCUserTime();
   213     }
   214     ///Gives back the ellapsed user time of the process' children
   215     double getCSystemTime() const
   216     {
   217       return operator TimeStamp().getCSystemTime();
   218     }
   219     ///Gives back the ellapsed real time of the process
   220     double getRealTime() const
   221     {
   222       return operator TimeStamp().getRealTime();
   223     }
   224 
   225   };
   226 
   227   ///Prints the time counters
   228 
   229   ///Prints the time counters in the following form:
   230   ///
   231   /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
   232   ///
   233   /// where the values are the
   234   /// \li \c u: user cpu time,
   235   /// \li \c s: system cpu time,
   236   /// \li \c cu: user cpu time of children,
   237   /// \li \c cs: system cpu time of children,
   238   /// \li \c real: real time.
   239   /// \relates TimeStamp
   240   inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
   241   {
   242     long cls = sysconf(_SC_CLK_TCK);
   243     os << "u: " << double(t.getTms().tms_utime)/cls <<
   244       "s, s: " << double(t.getTms().tms_stime)/cls <<
   245       "s, cu: " << double(t.getTms().tms_cutime)/cls <<
   246       "s, cs: " << double(t.getTms().tms_cstime)/cls <<
   247       "s, real: " << t.getRealTime() << "s";
   248     return os;
   249   }
   250 
   251   /// @}  
   252 
   253 } //namespace lemon
   254 
   255 #endif //LEMON_TIME_MEASURE_H