| 
alpar@209
 | 
     1  | 
/* -*- mode: C++; indent-tabs-mode: nil; -*-
  | 
| 
alpar@119
 | 
     2  | 
 *
  | 
| 
alpar@209
 | 
     3  | 
 * This file is a part of LEMON, a generic C++ optimization library.
  | 
| 
alpar@119
 | 
     4  | 
 *
  | 
| 
alpar@119
 | 
     5  | 
 * Copyright (C) 2003-2008
  | 
| 
alpar@119
 | 
     6  | 
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
  | 
| 
alpar@119
 | 
     7  | 
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
  | 
| 
alpar@119
 | 
     8  | 
 *
  | 
| 
alpar@119
 | 
     9  | 
 * Permission to use, modify and distribute this software is granted
  | 
| 
alpar@119
 | 
    10  | 
 * provided that this copyright notice appears in all copies. For
  | 
| 
alpar@119
 | 
    11  | 
 * precise terms see the accompanying LICENSE file.
  | 
| 
alpar@119
 | 
    12  | 
 *
  | 
| 
alpar@119
 | 
    13  | 
 * This software is provided "AS IS" with no warranty of any kind,
  | 
| 
alpar@119
 | 
    14  | 
 * express or implied, and with no claim as to its suitability for any
  | 
| 
alpar@119
 | 
    15  | 
 * purpose.
  | 
| 
alpar@119
 | 
    16  | 
 *
  | 
| 
alpar@119
 | 
    17  | 
 */
  | 
| 
alpar@119
 | 
    18  | 
  | 
| 
alpar@119
 | 
    19  | 
#ifndef LEMON_TIME_MEASURE_H
  | 
| 
alpar@119
 | 
    20  | 
#define LEMON_TIME_MEASURE_H
  | 
| 
alpar@119
 | 
    21  | 
  | 
| 
alpar@119
 | 
    22  | 
///\ingroup timecount
  | 
| 
alpar@119
 | 
    23  | 
///\file
  | 
| 
alpar@119
 | 
    24  | 
///\brief Tools for measuring cpu usage
  | 
| 
alpar@119
 | 
    25  | 
  | 
| 
deba@126
 | 
    26  | 
#ifdef WIN32
  | 
| 
alpar@143
 | 
    27  | 
#define WIN32_LEAN_AND_MEAN
  | 
| 
alpar@143
 | 
    28  | 
#define NOMINMAX
  | 
| 
deba@126
 | 
    29  | 
#include <windows.h>
  | 
| 
deba@126
 | 
    30  | 
#include <cmath>
  | 
| 
deba@126
 | 
    31  | 
#else
  | 
| 
alpar@119
 | 
    32  | 
#include <sys/times.h>
  | 
| 
deba@126
 | 
    33  | 
#include <sys/time.h>
  | 
| 
deba@126
 | 
    34  | 
#endif
  | 
| 
alpar@119
 | 
    35  | 
  | 
| 
alpar@143
 | 
    36  | 
#include <string>
  | 
| 
alpar@119
 | 
    37  | 
#include <fstream>
  | 
| 
alpar@119
 | 
    38  | 
#include <iostream>
  | 
| 
alpar@119
 | 
    39  | 
  | 
| 
alpar@119
 | 
    40  | 
namespace lemon {
 | 
| 
alpar@119
 | 
    41  | 
  | 
| 
alpar@119
 | 
    42  | 
  /// \addtogroup timecount
  | 
| 
alpar@119
 | 
    43  | 
  /// @{
 | 
| 
alpar@119
 | 
    44  | 
  | 
| 
alpar@119
 | 
    45  | 
  /// A class to store (cpu)time instances.
  | 
| 
alpar@119
 | 
    46  | 
  | 
| 
alpar@119
 | 
    47  | 
  /// This class stores five time values.
  | 
| 
alpar@119
 | 
    48  | 
  /// - a real time
  | 
| 
alpar@119
 | 
    49  | 
  /// - a user cpu time
  | 
| 
alpar@119
 | 
    50  | 
  /// - a system cpu time
  | 
| 
alpar@119
 | 
    51  | 
  /// - a user cpu time of children
  | 
| 
alpar@119
 | 
    52  | 
  /// - a system cpu time of children
  | 
| 
alpar@119
 | 
    53  | 
  ///
  | 
| 
alpar@119
 | 
    54  | 
  /// TimeStamp's can be added to or substracted from each other and
  | 
| 
alpar@119
 | 
    55  | 
  /// they can be pushed to a stream.
  | 
| 
alpar@119
 | 
    56  | 
  ///
  | 
| 
alpar@119
 | 
    57  | 
  /// In most cases, perhaps the \ref Timer or the \ref TimeReport
  | 
| 
alpar@119
 | 
    58  | 
  /// class is what you want to use instead.
  | 
| 
alpar@119
 | 
    59  | 
  | 
| 
alpar@119
 | 
    60  | 
  class TimeStamp
  | 
| 
alpar@119
 | 
    61  | 
  {
 | 
| 
deba@126
 | 
    62  | 
    double utime;
  | 
| 
deba@126
 | 
    63  | 
    double stime;
  | 
| 
deba@126
 | 
    64  | 
    double cutime;
  | 
| 
deba@126
 | 
    65  | 
    double cstime;
  | 
| 
deba@126
 | 
    66  | 
    double rtime;
  | 
| 
alpar@209
 | 
    67  | 
  | 
| 
alpar@209
 | 
    68  | 
    void _reset() {
 | 
| 
deba@126
 | 
    69  | 
      utime = stime = cutime = cstime = rtime = 0;
  | 
| 
alpar@119
 | 
    70  | 
    }
  | 
| 
alpar@119
 | 
    71  | 
  | 
| 
alpar@119
 | 
    72  | 
  public:
  | 
| 
alpar@119
 | 
    73  | 
  | 
| 
alpar@119
 | 
    74  | 
    ///Read the current time values of the process
  | 
| 
alpar@119
 | 
    75  | 
    void stamp()
  | 
| 
alpar@119
 | 
    76  | 
    {
 | 
| 
deba@126
 | 
    77  | 
#ifndef WIN32
  | 
| 
alpar@119
 | 
    78  | 
      timeval tv;
  | 
| 
deba@126
 | 
    79  | 
      gettimeofday(&tv, 0);
  | 
| 
deba@126
 | 
    80  | 
      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
  | 
| 
deba@126
 | 
    81  | 
  | 
| 
deba@126
 | 
    82  | 
      tms ts;
  | 
| 
deba@126
 | 
    83  | 
      double tck=sysconf(_SC_CLK_TCK);
  | 
| 
deba@126
 | 
    84  | 
      times(&ts);
  | 
| 
deba@126
 | 
    85  | 
      utime=ts.tms_utime/tck;
  | 
| 
deba@126
 | 
    86  | 
      stime=ts.tms_stime/tck;
  | 
| 
deba@126
 | 
    87  | 
      cutime=ts.tms_cutime/tck;
  | 
| 
deba@126
 | 
    88  | 
      cstime=ts.tms_cstime/tck;
  | 
| 
deba@126
 | 
    89  | 
#else
  | 
| 
deba@126
 | 
    90  | 
      static const double ch = 4294967296.0e-7;
  | 
| 
deba@126
 | 
    91  | 
      static const double cl = 1.0e-7;
  | 
| 
deba@126
 | 
    92  | 
  | 
| 
deba@126
 | 
    93  | 
      FILETIME system;
  | 
| 
deba@126
 | 
    94  | 
      GetSystemTimeAsFileTime(&system);
  | 
| 
deba@126
 | 
    95  | 
      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
  | 
| 
deba@126
 | 
    96  | 
  | 
| 
deba@126
 | 
    97  | 
      FILETIME create, exit, kernel, user;
  | 
| 
deba@126
 | 
    98  | 
      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
 | 
| 
alpar@209
 | 
    99  | 
        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
  | 
| 
alpar@209
 | 
   100  | 
        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
  | 
| 
alpar@209
 | 
   101  | 
        cutime = 0;
  | 
| 
alpar@209
 | 
   102  | 
        cstime = 0;
  | 
| 
deba@126
 | 
   103  | 
      } else {
 | 
| 
alpar@209
 | 
   104  | 
        rtime = 0;
  | 
| 
alpar@209
 | 
   105  | 
        utime = 0;
  | 
| 
alpar@209
 | 
   106  | 
        stime = 0;
  | 
| 
alpar@209
 | 
   107  | 
        cutime = 0;
  | 
| 
alpar@209
 | 
   108  | 
        cstime = 0;
  | 
| 
deba@126
 | 
   109  | 
      }
  | 
| 
alpar@209
 | 
   110  | 
#endif
  | 
| 
alpar@119
 | 
   111  | 
    }
  | 
| 
alpar@209
 | 
   112  | 
  | 
| 
alpar@119
 | 
   113  | 
    /// Constructor initializing with zero
  | 
| 
alpar@119
 | 
   114  | 
    TimeStamp()
  | 
| 
alpar@119
 | 
   115  | 
    { _reset(); }
 | 
| 
alpar@119
 | 
   116  | 
    ///Constructor initializing with the current time values of the process
  | 
| 
alpar@119
 | 
   117  | 
    TimeStamp(void *) { stamp();}
 | 
| 
alpar@209
 | 
   118  | 
  | 
| 
alpar@119
 | 
   119  | 
    ///Set every time value to zero
  | 
| 
alpar@119
 | 
   120  | 
    TimeStamp &reset() {_reset();return *this;}
 | 
| 
alpar@119
 | 
   121  | 
  | 
| 
alpar@119
 | 
   122  | 
    ///\e
  | 
| 
alpar@119
 | 
   123  | 
    TimeStamp &operator+=(const TimeStamp &b)
  | 
| 
alpar@119
 | 
   124  | 
    {
 | 
| 
deba@126
 | 
   125  | 
      utime+=b.utime;
  | 
| 
deba@126
 | 
   126  | 
      stime+=b.stime;
  | 
| 
deba@126
 | 
   127  | 
      cutime+=b.cutime;
  | 
| 
deba@126
 | 
   128  | 
      cstime+=b.cstime;
  | 
| 
deba@126
 | 
   129  | 
      rtime+=b.rtime;
  | 
| 
alpar@119
 | 
   130  | 
      return *this;
  | 
| 
alpar@119
 | 
   131  | 
    }
  | 
| 
alpar@119
 | 
   132  | 
    ///\e
  | 
| 
alpar@119
 | 
   133  | 
    TimeStamp operator+(const TimeStamp &b) const
  | 
| 
alpar@119
 | 
   134  | 
    {
 | 
| 
alpar@119
 | 
   135  | 
      TimeStamp t(*this);
  | 
| 
alpar@119
 | 
   136  | 
      return t+=b;
  | 
| 
alpar@119
 | 
   137  | 
    }
  | 
| 
alpar@119
 | 
   138  | 
    ///\e
  | 
| 
alpar@119
 | 
   139  | 
    TimeStamp &operator-=(const TimeStamp &b)
  | 
| 
alpar@119
 | 
   140  | 
    {
 | 
| 
deba@126
 | 
   141  | 
      utime-=b.utime;
  | 
| 
deba@126
 | 
   142  | 
      stime-=b.stime;
  | 
| 
deba@126
 | 
   143  | 
      cutime-=b.cutime;
  | 
| 
deba@126
 | 
   144  | 
      cstime-=b.cstime;
  | 
| 
deba@126
 | 
   145  | 
      rtime-=b.rtime;
  | 
| 
alpar@119
 | 
   146  | 
      return *this;
  | 
| 
alpar@119
 | 
   147  | 
    }
  | 
| 
alpar@119
 | 
   148  | 
    ///\e
  | 
| 
alpar@119
 | 
   149  | 
    TimeStamp operator-(const TimeStamp &b) const
  | 
| 
alpar@119
 | 
   150  | 
    {
 | 
| 
alpar@119
 | 
   151  | 
      TimeStamp t(*this);
  | 
| 
alpar@119
 | 
   152  | 
      return t-=b;
  | 
| 
alpar@119
 | 
   153  | 
    }
  | 
| 
alpar@119
 | 
   154  | 
    ///\e
  | 
| 
alpar@119
 | 
   155  | 
    TimeStamp &operator*=(double b)
  | 
| 
alpar@119
 | 
   156  | 
    {
 | 
| 
deba@126
 | 
   157  | 
      utime*=b;
  | 
| 
deba@126
 | 
   158  | 
      stime*=b;
  | 
| 
deba@126
 | 
   159  | 
      cutime*=b;
  | 
| 
deba@126
 | 
   160  | 
      cstime*=b;
  | 
| 
deba@126
 | 
   161  | 
      rtime*=b;
  | 
| 
alpar@119
 | 
   162  | 
      return *this;
  | 
| 
alpar@119
 | 
   163  | 
    }
  | 
| 
alpar@119
 | 
   164  | 
    ///\e
  | 
| 
alpar@119
 | 
   165  | 
    TimeStamp operator*(double b) const
  | 
| 
alpar@119
 | 
   166  | 
    {
 | 
| 
alpar@119
 | 
   167  | 
      TimeStamp t(*this);
  | 
| 
alpar@119
 | 
   168  | 
      return t*=b;
  | 
| 
alpar@119
 | 
   169  | 
    }
  | 
| 
alpar@119
 | 
   170  | 
    friend TimeStamp operator*(double b,const TimeStamp &t);
  | 
| 
alpar@119
 | 
   171  | 
    ///\e
  | 
| 
alpar@119
 | 
   172  | 
    TimeStamp &operator/=(double b)
  | 
| 
alpar@119
 | 
   173  | 
    {
 | 
| 
deba@126
 | 
   174  | 
      utime/=b;
  | 
| 
deba@126
 | 
   175  | 
      stime/=b;
  | 
| 
deba@126
 | 
   176  | 
      cutime/=b;
  | 
| 
deba@126
 | 
   177  | 
      cstime/=b;
  | 
| 
deba@126
 | 
   178  | 
      rtime/=b;
  | 
| 
alpar@119
 | 
   179  | 
      return *this;
  | 
| 
alpar@119
 | 
   180  | 
    }
  | 
| 
alpar@119
 | 
   181  | 
    ///\e
  | 
| 
alpar@119
 | 
   182  | 
    TimeStamp operator/(double b) const
  | 
| 
alpar@119
 | 
   183  | 
    {
 | 
| 
alpar@119
 | 
   184  | 
      TimeStamp t(*this);
  | 
| 
alpar@119
 | 
   185  | 
      return t/=b;
  | 
| 
alpar@119
 | 
   186  | 
    }
  | 
| 
alpar@119
 | 
   187  | 
    ///The time ellapsed since the last call of stamp()
  | 
| 
alpar@119
 | 
   188  | 
    TimeStamp ellapsed() const
  | 
| 
alpar@119
 | 
   189  | 
    {
 | 
| 
alpar@119
 | 
   190  | 
      TimeStamp t(NULL);
  | 
| 
alpar@119
 | 
   191  | 
      return t-*this;
  | 
| 
alpar@119
 | 
   192  | 
    }
  | 
| 
alpar@209
 | 
   193  | 
  | 
| 
alpar@119
 | 
   194  | 
    friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
  | 
| 
alpar@209
 | 
   195  | 
  | 
| 
alpar@119
 | 
   196  | 
    ///Gives back the user time of the process
  | 
| 
alpar@119
 | 
   197  | 
    double userTime() const
  | 
| 
alpar@119
 | 
   198  | 
    {
 | 
| 
deba@126
 | 
   199  | 
      return utime;
  | 
| 
alpar@119
 | 
   200  | 
    }
  | 
| 
alpar@119
 | 
   201  | 
    ///Gives back the system time of the process
  | 
| 
alpar@119
 | 
   202  | 
    double systemTime() const
  | 
| 
alpar@119
 | 
   203  | 
    {
 | 
| 
deba@126
 | 
   204  | 
      return stime;
  | 
| 
alpar@119
 | 
   205  | 
    }
  | 
| 
alpar@119
 | 
   206  | 
    ///Gives back the user time of the process' children
  | 
| 
deba@126
 | 
   207  | 
  | 
| 
alpar@209
 | 
   208  | 
    ///\note On <tt>WIN32</tt> platform this value is not calculated.
  | 
| 
deba@126
 | 
   209  | 
    ///
  | 
| 
alpar@119
 | 
   210  | 
    double cUserTime() const
  | 
| 
alpar@119
 | 
   211  | 
    {
 | 
| 
deba@126
 | 
   212  | 
      return cutime;
  | 
| 
alpar@119
 | 
   213  | 
    }
  | 
| 
alpar@119
 | 
   214  | 
    ///Gives back the user time of the process' children
  | 
| 
deba@126
 | 
   215  | 
  | 
| 
alpar@209
 | 
   216  | 
    ///\note On <tt>WIN32</tt> platform this value is not calculated.
  | 
| 
deba@126
 | 
   217  | 
    ///
  | 
| 
alpar@119
 | 
   218  | 
    double cSystemTime() const
  | 
| 
alpar@119
 | 
   219  | 
    {
 | 
| 
deba@126
 | 
   220  | 
      return cstime;
  | 
| 
alpar@119
 | 
   221  | 
    }
  | 
| 
alpar@119
 | 
   222  | 
    ///Gives back the real time
  | 
| 
deba@126
 | 
   223  | 
    double realTime() const {return rtime;}
 | 
| 
alpar@119
 | 
   224  | 
  };
  | 
| 
alpar@119
 | 
   225  | 
  | 
| 
alpar@209
 | 
   226  | 
  TimeStamp operator*(double b,const TimeStamp &t)
  | 
| 
alpar@119
 | 
   227  | 
  {
 | 
| 
alpar@119
 | 
   228  | 
    return t*b;
  | 
| 
alpar@119
 | 
   229  | 
  }
  | 
| 
alpar@209
 | 
   230  | 
  | 
| 
alpar@119
 | 
   231  | 
  ///Prints the time counters
  | 
| 
alpar@119
 | 
   232  | 
  | 
| 
alpar@119
 | 
   233  | 
  ///Prints the time counters in the following form:
  | 
| 
alpar@119
 | 
   234  | 
  ///
  | 
| 
alpar@119
 | 
   235  | 
  /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
  | 
| 
alpar@119
 | 
   236  | 
  ///
  | 
| 
alpar@119
 | 
   237  | 
  /// where the values are the
  | 
| 
alpar@119
 | 
   238  | 
  /// \li \c u: user cpu time,
  | 
| 
alpar@119
 | 
   239  | 
  /// \li \c s: system cpu time,
  | 
| 
alpar@119
 | 
   240  | 
  /// \li \c cu: user cpu time of children,
  | 
| 
alpar@119
 | 
   241  | 
  /// \li \c cs: system cpu time of children,
  | 
| 
alpar@119
 | 
   242  | 
  /// \li \c real: real time.
  | 
| 
alpar@119
 | 
   243  | 
  /// \relates TimeStamp
  | 
| 
deba@126
 | 
   244  | 
  /// \note On <tt>WIN32</tt> platform the cummulative values are not
  | 
| 
deba@126
 | 
   245  | 
  /// calculated.
  | 
| 
alpar@119
 | 
   246  | 
  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
  | 
| 
alpar@119
 | 
   247  | 
  {
 | 
| 
deba@126
 | 
   248  | 
    os << "u: " << t.userTime() <<
  | 
| 
deba@126
 | 
   249  | 
      "s, s: " << t.systemTime() <<
  | 
| 
deba@126
 | 
   250  | 
      "s, cu: " << t.cUserTime() <<
  | 
| 
deba@126
 | 
   251  | 
      "s, cs: " << t.cSystemTime() <<
  | 
| 
alpar@119
 | 
   252  | 
      "s, real: " << t.realTime() << "s";
  | 
| 
alpar@119
 | 
   253  | 
    return os;
  | 
| 
alpar@119
 | 
   254  | 
  }
  | 
| 
alpar@119
 | 
   255  | 
  | 
| 
alpar@119
 | 
   256  | 
  ///Class for measuring the cpu time and real time usage of the process
  | 
| 
alpar@119
 | 
   257  | 
  | 
| 
alpar@119
 | 
   258  | 
  ///Class for measuring the cpu time and real time usage of the process.
  | 
| 
alpar@119
 | 
   259  | 
  ///It is quite easy-to-use, here is a short example.
  | 
| 
alpar@119
 | 
   260  | 
  ///\code
  | 
| 
alpar@119
 | 
   261  | 
  /// #include<lemon/time_measure.h>
  | 
| 
alpar@119
 | 
   262  | 
  /// #include<iostream>
  | 
| 
alpar@119
 | 
   263  | 
  ///
  | 
| 
alpar@119
 | 
   264  | 
  /// int main()
  | 
| 
alpar@119
 | 
   265  | 
  /// {
 | 
| 
alpar@119
 | 
   266  | 
  ///
  | 
| 
alpar@119
 | 
   267  | 
  ///   ...
  | 
| 
alpar@119
 | 
   268  | 
  ///
  | 
| 
alpar@119
 | 
   269  | 
  ///   Timer t;
  | 
| 
alpar@119
 | 
   270  | 
  ///   doSomething();
  | 
| 
alpar@119
 | 
   271  | 
  ///   std::cout << t << '\n';
  | 
| 
alpar@119
 | 
   272  | 
  ///   t.restart();
  | 
| 
alpar@119
 | 
   273  | 
  ///   doSomethingElse();
  | 
| 
alpar@119
 | 
   274  | 
  ///   std::cout << t << '\n';
  | 
| 
alpar@119
 | 
   275  | 
  ///
  | 
| 
alpar@119
 | 
   276  | 
  ///   ...
  | 
| 
alpar@119
 | 
   277  | 
  ///
  | 
| 
alpar@119
 | 
   278  | 
  /// }
  | 
| 
alpar@119
 | 
   279  | 
  ///\endcode
  | 
| 
alpar@119
 | 
   280  | 
  ///
  | 
| 
alpar@119
 | 
   281  | 
  ///The \ref Timer can also be \ref stop() "stopped" and
  | 
| 
alpar@119
 | 
   282  | 
  ///\ref start() "started" again, so it is possible to compute collected
  | 
| 
alpar@119
 | 
   283  | 
  ///running times.
  | 
| 
alpar@119
 | 
   284  | 
  ///
  | 
| 
alpar@119
 | 
   285  | 
  ///\warning Depending on the operation system and its actual configuration
  | 
| 
alpar@119
 | 
   286  | 
  ///the time counters have a certain (10ms on a typical Linux system)
  | 
| 
alpar@119
 | 
   287  | 
  ///granularity.
  | 
| 
alpar@119
 | 
   288  | 
  ///Therefore this tool is not appropriate to measure very short times.
  | 
| 
alpar@119
 | 
   289  | 
  ///Also, if you start and stop the timer very frequently, it could lead to
  | 
| 
alpar@119
 | 
   290  | 
  ///distorted results.
  | 
| 
alpar@119
 | 
   291  | 
  ///
  | 
| 
alpar@119
 | 
   292  | 
  ///\note If you want to measure the running time of the execution of a certain
  | 
| 
alpar@119
 | 
   293  | 
  ///function, consider the usage of \ref TimeReport instead.
  | 
| 
alpar@119
 | 
   294  | 
  ///
  | 
| 
alpar@119
 | 
   295  | 
  ///\sa TimeReport
  | 
| 
alpar@119
 | 
   296  | 
  class Timer
  | 
| 
alpar@119
 | 
   297  | 
  {
 | 
| 
alpar@119
 | 
   298  | 
    int _running; //Timer is running iff _running>0; (_running>=0 always holds)
  | 
| 
alpar@119
 | 
   299  | 
    TimeStamp start_time; //This is the relativ start-time if the timer
  | 
| 
alpar@119
 | 
   300  | 
                          //is _running, the collected _running time otherwise.
  | 
| 
alpar@209
 | 
   301  | 
  | 
| 
alpar@119
 | 
   302  | 
    void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
 | 
| 
alpar@209
 | 
   303  | 
  | 
| 
alpar@209
 | 
   304  | 
  public:
  | 
| 
alpar@119
 | 
   305  | 
    ///Constructor.
  | 
| 
alpar@119
 | 
   306  | 
  | 
| 
alpar@119
 | 
   307  | 
    ///\param run indicates whether or not the timer starts immediately.
  | 
| 
alpar@119
 | 
   308  | 
    ///
  | 
| 
alpar@119
 | 
   309  | 
    Timer(bool run=true) :_running(run) {_reset();}
 | 
| 
alpar@119
 | 
   310  | 
  | 
| 
alpar@119
 | 
   311  | 
    ///\name Control the state of the timer
  | 
| 
alpar@119
 | 
   312  | 
    ///Basically a Timer can be either running or stopped,
  | 
| 
alpar@119
 | 
   313  | 
    ///but it provides a bit finer control on the execution.
  | 
| 
kpeter@314
 | 
   314  | 
    ///The \ref lemon::Timer "Timer" also counts the number of
  | 
| 
kpeter@314
 | 
   315  | 
    ///\ref lemon::Timer::start() "start()" executions, and it stops
  | 
| 
kpeter@313
 | 
   316  | 
    ///only after the same amount (or more) \ref lemon::Timer::stop()
  | 
| 
kpeter@313
 | 
   317  | 
    ///"stop()"s. This can be useful e.g. to compute the running time
  | 
| 
alpar@119
 | 
   318  | 
    ///of recursive functions.
  | 
| 
alpar@119
 | 
   319  | 
  | 
| 
alpar@119
 | 
   320  | 
    ///@{
 | 
| 
alpar@119
 | 
   321  | 
  | 
| 
alpar@119
 | 
   322  | 
    ///Reset and stop the time counters
  | 
| 
alpar@119
 | 
   323  | 
  | 
| 
alpar@119
 | 
   324  | 
    ///This function resets and stops the time counters
  | 
| 
alpar@119
 | 
   325  | 
    ///\sa restart()
  | 
| 
alpar@119
 | 
   326  | 
    void reset()
  | 
| 
alpar@119
 | 
   327  | 
    {
 | 
| 
alpar@119
 | 
   328  | 
      _running=0;
  | 
| 
alpar@119
 | 
   329  | 
      _reset();
  | 
| 
alpar@119
 | 
   330  | 
    }
  | 
| 
alpar@119
 | 
   331  | 
  | 
| 
alpar@119
 | 
   332  | 
    ///Start the time counters
  | 
| 
alpar@209
 | 
   333  | 
  | 
| 
alpar@119
 | 
   334  | 
    ///This function starts the time counters.
  | 
| 
alpar@119
 | 
   335  | 
    ///
  | 
| 
alpar@119
 | 
   336  | 
    ///If the timer is started more than ones, it will remain running
  | 
| 
alpar@119
 | 
   337  | 
    ///until the same amount of \ref stop() is called.
  | 
| 
alpar@119
 | 
   338  | 
    ///\sa stop()
  | 
| 
alpar@209
 | 
   339  | 
    void start()
  | 
| 
alpar@119
 | 
   340  | 
    {
 | 
| 
alpar@119
 | 
   341  | 
      if(_running) _running++;
  | 
| 
alpar@119
 | 
   342  | 
      else {
 | 
| 
alpar@209
 | 
   343  | 
        _running=1;
  | 
| 
alpar@209
 | 
   344  | 
        TimeStamp t;
  | 
| 
alpar@209
 | 
   345  | 
        t.stamp();
  | 
| 
alpar@209
 | 
   346  | 
        start_time=t-start_time;
  | 
| 
alpar@119
 | 
   347  | 
      }
  | 
| 
alpar@119
 | 
   348  | 
    }
  | 
| 
alpar@119
 | 
   349  | 
  | 
| 
alpar@209
 | 
   350  | 
  | 
| 
alpar@119
 | 
   351  | 
    ///Stop the time counters
  | 
| 
alpar@119
 | 
   352  | 
  | 
| 
alpar@119
 | 
   353  | 
    ///This function stops the time counters. If start() was executed more than
  | 
| 
alpar@119
 | 
   354  | 
    ///once, then the same number of stop() execution is necessary the really
  | 
| 
alpar@119
 | 
   355  | 
    ///stop the timer.
  | 
| 
alpar@209
 | 
   356  | 
    ///
  | 
| 
alpar@119
 | 
   357  | 
    ///\sa halt()
  | 
| 
alpar@119
 | 
   358  | 
    ///\sa start()
  | 
| 
alpar@119
 | 
   359  | 
    ///\sa restart()
  | 
| 
alpar@119
 | 
   360  | 
    ///\sa reset()
  | 
| 
alpar@119
 | 
   361  | 
  | 
| 
alpar@209
 | 
   362  | 
    void stop()
  | 
| 
alpar@119
 | 
   363  | 
    {
 | 
| 
alpar@119
 | 
   364  | 
      if(_running && !--_running) {
 | 
| 
alpar@209
 | 
   365  | 
        TimeStamp t;
  | 
| 
alpar@209
 | 
   366  | 
        t.stamp();
  | 
| 
alpar@209
 | 
   367  | 
        start_time=t-start_time;
  | 
| 
alpar@119
 | 
   368  | 
      }
  | 
| 
alpar@119
 | 
   369  | 
    }
  | 
| 
alpar@119
 | 
   370  | 
  | 
| 
alpar@119
 | 
   371  | 
    ///Halt (i.e stop immediately) the time counters
  | 
| 
alpar@119
 | 
   372  | 
  | 
| 
alpar@120
 | 
   373  | 
    ///This function stops immediately the time counters, i.e. <tt>t.halt()</tt>
  | 
| 
alpar@119
 | 
   374  | 
    ///is a faster
  | 
| 
alpar@119
 | 
   375  | 
    ///equivalent of the following.
  | 
| 
alpar@119
 | 
   376  | 
    ///\code
  | 
| 
alpar@119
 | 
   377  | 
    ///  while(t.running()) t.stop()
  | 
| 
alpar@119
 | 
   378  | 
    ///\endcode
  | 
| 
alpar@119
 | 
   379  | 
    ///
  | 
| 
alpar@119
 | 
   380  | 
    ///
  | 
| 
alpar@119
 | 
   381  | 
    ///\sa stop()
  | 
| 
alpar@119
 | 
   382  | 
    ///\sa restart()
  | 
| 
alpar@119
 | 
   383  | 
    ///\sa reset()
  | 
| 
alpar@119
 | 
   384  | 
  | 
| 
alpar@209
 | 
   385  | 
    void halt()
  | 
| 
alpar@119
 | 
   386  | 
    {
 | 
| 
alpar@119
 | 
   387  | 
      if(_running) {
 | 
| 
alpar@209
 | 
   388  | 
        _running=0;
  | 
| 
alpar@209
 | 
   389  | 
        TimeStamp t;
  | 
| 
alpar@209
 | 
   390  | 
        t.stamp();
  | 
| 
alpar@209
 | 
   391  | 
        start_time=t-start_time;
  | 
| 
alpar@119
 | 
   392  | 
      }
  | 
| 
alpar@119
 | 
   393  | 
    }
  | 
| 
alpar@119
 | 
   394  | 
  | 
| 
alpar@119
 | 
   395  | 
    ///Returns the running state of the timer
  | 
| 
alpar@119
 | 
   396  | 
  | 
| 
alpar@119
 | 
   397  | 
    ///This function returns the number of stop() exections that is
  | 
| 
alpar@119
 | 
   398  | 
    ///necessary to really stop the timer.
  | 
| 
alpar@119
 | 
   399  | 
    ///For example the timer
  | 
| 
alpar@119
 | 
   400  | 
    ///is running if and only if the return value is \c true
  | 
| 
alpar@119
 | 
   401  | 
    ///(i.e. greater than
  | 
| 
alpar@119
 | 
   402  | 
    ///zero).
  | 
| 
alpar@119
 | 
   403  | 
    int running()  { return _running; }
 | 
| 
alpar@209
 | 
   404  | 
  | 
| 
alpar@209
 | 
   405  | 
  | 
| 
alpar@119
 | 
   406  | 
    ///Restart the time counters
  | 
| 
alpar@119
 | 
   407  | 
  | 
| 
alpar@119
 | 
   408  | 
    ///This function is a shorthand for
  | 
| 
alpar@119
 | 
   409  | 
    ///a reset() and a start() calls.
  | 
| 
alpar@119
 | 
   410  | 
    ///
  | 
| 
alpar@209
 | 
   411  | 
    void restart()
  | 
| 
alpar@119
 | 
   412  | 
    {
 | 
| 
alpar@119
 | 
   413  | 
      reset();
  | 
| 
alpar@119
 | 
   414  | 
      start();
  | 
| 
alpar@119
 | 
   415  | 
    }
  | 
| 
alpar@209
 | 
   416  | 
  | 
| 
alpar@119
 | 
   417  | 
    ///@}
  | 
| 
alpar@119
 | 
   418  | 
  | 
| 
alpar@119
 | 
   419  | 
    ///\name Query Functions for the ellapsed time
  | 
| 
alpar@119
 | 
   420  | 
  | 
| 
alpar@119
 | 
   421  | 
    ///@{
 | 
| 
alpar@119
 | 
   422  | 
  | 
| 
alpar@119
 | 
   423  | 
    ///Gives back the ellapsed user time of the process
  | 
| 
alpar@119
 | 
   424  | 
    double userTime() const
  | 
| 
alpar@119
 | 
   425  | 
    {
 | 
| 
alpar@119
 | 
   426  | 
      return operator TimeStamp().userTime();
  | 
| 
alpar@119
 | 
   427  | 
    }
  | 
| 
alpar@119
 | 
   428  | 
    ///Gives back the ellapsed system time of the process
  | 
| 
alpar@119
 | 
   429  | 
    double systemTime() const
  | 
| 
alpar@119
 | 
   430  | 
    {
 | 
| 
alpar@119
 | 
   431  | 
      return operator TimeStamp().systemTime();
  | 
| 
alpar@119
 | 
   432  | 
    }
  | 
| 
alpar@119
 | 
   433  | 
    ///Gives back the ellapsed user time of the process' children
  | 
| 
deba@126
 | 
   434  | 
  | 
| 
alpar@209
 | 
   435  | 
    ///\note On <tt>WIN32</tt> platform this value is not calculated.
  | 
| 
deba@126
 | 
   436  | 
    ///
  | 
| 
alpar@119
 | 
   437  | 
    double cUserTime() const
  | 
| 
alpar@119
 | 
   438  | 
    {
 | 
| 
alpar@119
 | 
   439  | 
      return operator TimeStamp().cUserTime();
  | 
| 
alpar@119
 | 
   440  | 
    }
  | 
| 
alpar@119
 | 
   441  | 
    ///Gives back the ellapsed user time of the process' children
  | 
| 
deba@126
 | 
   442  | 
  | 
| 
alpar@209
 | 
   443  | 
    ///\note On <tt>WIN32</tt> platform this value is not calculated.
  | 
| 
deba@126
 | 
   444  | 
    ///
  | 
| 
alpar@119
 | 
   445  | 
    double cSystemTime() const
  | 
| 
alpar@119
 | 
   446  | 
    {
 | 
| 
alpar@119
 | 
   447  | 
      return operator TimeStamp().cSystemTime();
  | 
| 
alpar@119
 | 
   448  | 
    }
  | 
| 
alpar@119
 | 
   449  | 
    ///Gives back the ellapsed real time
  | 
| 
alpar@119
 | 
   450  | 
    double realTime() const
  | 
| 
alpar@119
 | 
   451  | 
    {
 | 
| 
alpar@119
 | 
   452  | 
      return operator TimeStamp().realTime();
  | 
| 
alpar@119
 | 
   453  | 
    }
  | 
| 
alpar@119
 | 
   454  | 
    ///Computes the ellapsed time
  | 
| 
alpar@119
 | 
   455  | 
  | 
| 
alpar@119
 | 
   456  | 
    ///This conversion computes the ellapsed time, therefore you can print
  | 
| 
alpar@119
 | 
   457  | 
    ///the ellapsed time like this.
  | 
| 
alpar@119
 | 
   458  | 
    ///\code
  | 
| 
alpar@119
 | 
   459  | 
    ///  Timer t;
  | 
| 
alpar@119
 | 
   460  | 
    ///  doSomething();
  | 
| 
alpar@119
 | 
   461  | 
    ///  std::cout << t << '\n';
  | 
| 
alpar@119
 | 
   462  | 
    ///\endcode
  | 
| 
alpar@119
 | 
   463  | 
    operator TimeStamp () const
  | 
| 
alpar@119
 | 
   464  | 
    {
 | 
| 
alpar@119
 | 
   465  | 
      TimeStamp t;
  | 
| 
alpar@119
 | 
   466  | 
      t.stamp();
  | 
| 
alpar@119
 | 
   467  | 
      return _running?t-start_time:start_time;
  | 
| 
alpar@119
 | 
   468  | 
    }
  | 
| 
alpar@119
 | 
   469  | 
  | 
| 
alpar@119
 | 
   470  | 
  | 
| 
alpar@119
 | 
   471  | 
    ///@}
  | 
| 
alpar@119
 | 
   472  | 
  };
  | 
| 
alpar@119
 | 
   473  | 
  | 
| 
kpeter@313
 | 
   474  | 
  ///Same as Timer but prints a report on destruction.
  | 
| 
alpar@119
 | 
   475  | 
  | 
| 
alpar@119
 | 
   476  | 
  ///Same as \ref Timer but prints a report on destruction.
  | 
| 
alpar@119
 | 
   477  | 
  ///This example shows its usage.
  | 
| 
alpar@119
 | 
   478  | 
  ///\code
  | 
| 
alpar@119
 | 
   479  | 
  ///  void myAlg(ListGraph &g,int n)
  | 
| 
alpar@119
 | 
   480  | 
  ///  {
 | 
| 
alpar@119
 | 
   481  | 
  ///    TimeReport tr("Running time of myAlg: ");
 | 
| 
alpar@119
 | 
   482  | 
  ///    ... //Here comes the algorithm
  | 
| 
alpar@119
 | 
   483  | 
  ///  }
  | 
| 
alpar@119
 | 
   484  | 
  ///\endcode
  | 
| 
alpar@119
 | 
   485  | 
  ///
  | 
| 
alpar@119
 | 
   486  | 
  ///\sa Timer
  | 
| 
alpar@119
 | 
   487  | 
  ///\sa NoTimeReport
  | 
| 
alpar@209
 | 
   488  | 
  class TimeReport : public Timer
  | 
| 
alpar@119
 | 
   489  | 
  {
 | 
| 
alpar@119
 | 
   490  | 
    std::string _title;
  | 
| 
alpar@119
 | 
   491  | 
    std::ostream &_os;
  | 
| 
alpar@119
 | 
   492  | 
  public:
  | 
| 
kpeter@313
 | 
   493  | 
    ///Constructor
  | 
| 
alpar@119
 | 
   494  | 
  | 
| 
kpeter@313
 | 
   495  | 
    ///Constructor.
  | 
| 
alpar@119
 | 
   496  | 
    ///\param title This text will be printed before the ellapsed time.
  | 
| 
alpar@119
 | 
   497  | 
    ///\param os The stream to print the report to.
  | 
| 
alpar@119
 | 
   498  | 
    ///\param run Sets whether the timer should start immediately.
  | 
| 
alpar@209
 | 
   499  | 
    TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
  | 
| 
alpar@119
 | 
   500  | 
      : Timer(run), _title(title), _os(os){}
 | 
| 
kpeter@313
 | 
   501  | 
    ///Destructor that prints the ellapsed time
  | 
| 
alpar@209
 | 
   502  | 
    ~TimeReport()
  | 
| 
alpar@119
 | 
   503  | 
    {
 | 
| 
alpar@119
 | 
   504  | 
      _os << _title << *this << std::endl;
  | 
| 
alpar@119
 | 
   505  | 
    }
  | 
| 
alpar@119
 | 
   506  | 
  };
  | 
| 
alpar@209
 | 
   507  | 
  | 
| 
kpeter@313
 | 
   508  | 
  ///'Do nothing' version of TimeReport
  | 
| 
alpar@119
 | 
   509  | 
  | 
| 
alpar@119
 | 
   510  | 
  ///\sa TimeReport
  | 
| 
alpar@119
 | 
   511  | 
  ///
  | 
| 
alpar@119
 | 
   512  | 
  class NoTimeReport
  | 
| 
alpar@119
 | 
   513  | 
  {
 | 
| 
alpar@119
 | 
   514  | 
  public:
  | 
| 
alpar@119
 | 
   515  | 
    ///\e
  | 
| 
alpar@119
 | 
   516  | 
    NoTimeReport(std::string,std::ostream &,bool) {}
 | 
| 
alpar@119
 | 
   517  | 
    ///\e
  | 
| 
alpar@119
 | 
   518  | 
    NoTimeReport(std::string,std::ostream &) {}
 | 
| 
alpar@119
 | 
   519  | 
    ///\e
  | 
| 
alpar@119
 | 
   520  | 
    NoTimeReport(std::string) {}
 | 
| 
alpar@119
 | 
   521  | 
    ///\e Do nothing.
  | 
| 
alpar@119
 | 
   522  | 
    ~NoTimeReport() {}
 | 
| 
alpar@119
 | 
   523  | 
  | 
| 
alpar@119
 | 
   524  | 
    operator TimeStamp () const { return TimeStamp(); }
 | 
| 
alpar@119
 | 
   525  | 
    void reset() {}
 | 
| 
alpar@119
 | 
   526  | 
    void start() {}
 | 
| 
alpar@119
 | 
   527  | 
    void stop() {}
 | 
| 
alpar@209
 | 
   528  | 
    void halt() {}
 | 
| 
alpar@119
 | 
   529  | 
    int running() { return 0; }
 | 
| 
alpar@119
 | 
   530  | 
    void restart() {}
 | 
| 
alpar@119
 | 
   531  | 
    double userTime() const { return 0; }
 | 
| 
alpar@119
 | 
   532  | 
    double systemTime() const { return 0; }
 | 
| 
alpar@119
 | 
   533  | 
    double cUserTime() const { return 0; }
 | 
| 
alpar@119
 | 
   534  | 
    double cSystemTime() const { return 0; }
 | 
| 
alpar@119
 | 
   535  | 
    double realTime() const { return 0; }
 | 
| 
alpar@119
 | 
   536  | 
  };
  | 
| 
alpar@209
 | 
   537  | 
  | 
| 
alpar@119
 | 
   538  | 
  ///Tool to measure the running time more exactly.
  | 
| 
alpar@209
 | 
   539  | 
  | 
| 
alpar@119
 | 
   540  | 
  ///This function calls \c f several times and returns the average
  | 
| 
alpar@119
 | 
   541  | 
  ///running time. The number of the executions will be choosen in such a way
  | 
| 
alpar@119
 | 
   542  | 
  ///that the full real running time will be roughly between \c min_time
  | 
| 
alpar@119
 | 
   543  | 
  ///and <tt>2*min_time</tt>.
  | 
| 
alpar@119
 | 
   544  | 
  ///\param f the function object to be measured.
  | 
| 
alpar@119
 | 
   545  | 
  ///\param min_time the minimum total running time.
  | 
| 
alpar@119
 | 
   546  | 
  ///\retval num if it is not \c NULL, then the actual
  | 
| 
alpar@119
 | 
   547  | 
  ///        number of execution of \c f will be written into <tt>*num</tt>.
  | 
| 
alpar@119
 | 
   548  | 
  ///\retval full_time if it is not \c NULL, then the actual
  | 
| 
alpar@119
 | 
   549  | 
  ///        total running time will be written into <tt>*full_time</tt>.
  | 
| 
alpar@119
 | 
   550  | 
  ///\return The average running time of \c f.
  | 
| 
alpar@209
 | 
   551  | 
  | 
| 
alpar@119
 | 
   552  | 
  template<class F>
  | 
| 
alpar@119
 | 
   553  | 
  TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL,
  | 
| 
alpar@119
 | 
   554  | 
                            TimeStamp *full_time=NULL)
  | 
| 
alpar@119
 | 
   555  | 
  {
 | 
| 
alpar@119
 | 
   556  | 
    TimeStamp full;
  | 
| 
alpar@119
 | 
   557  | 
    unsigned int total=0;
  | 
| 
alpar@119
 | 
   558  | 
    Timer t;
  | 
| 
alpar@119
 | 
   559  | 
    for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) {
 | 
| 
alpar@119
 | 
   560  | 
      for(;total<tn;total++) f();
  | 
| 
alpar@119
 | 
   561  | 
      full=t;
  | 
| 
alpar@119
 | 
   562  | 
    }
  | 
| 
alpar@119
 | 
   563  | 
    if(num) *num=total;
  | 
| 
alpar@119
 | 
   564  | 
    if(full_time) *full_time=full;
  | 
| 
alpar@119
 | 
   565  | 
    return full/total;
  | 
| 
alpar@119
 | 
   566  | 
  }
  | 
| 
alpar@209
 | 
   567  | 
  | 
| 
alpar@209
 | 
   568  | 
  /// @}
  | 
| 
alpar@119
 | 
   569  | 
  | 
| 
alpar@119
 | 
   570  | 
  | 
| 
alpar@119
 | 
   571  | 
} //namespace lemon
  | 
| 
alpar@119
 | 
   572  | 
  | 
| 
alpar@119
 | 
   573  | 
#endif //LEMON_TIME_MEASURE_H
  |