lemon/time_measure.h
changeset 1184 3c00344f49c9
parent 1092 dceba191c00d
     1.1 --- a/lemon/time_measure.h	Mon Jul 16 16:21:40 2018 +0200
     1.2 +++ b/lemon/time_measure.h	Wed Oct 17 19:14:07 2018 +0200
     1.3 @@ -2,7 +2,7 @@
     1.4   *
     1.5   * This file is a part of LEMON, a generic C++ optimization library.
     1.6   *
     1.7 - * Copyright (C) 2003-2009
     1.8 + * Copyright (C) 2003-2013
     1.9   * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10   * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11   *
    1.12 @@ -23,7 +23,9 @@
    1.13  ///\file
    1.14  ///\brief Tools for measuring cpu usage
    1.15  
    1.16 -#ifdef WIN32
    1.17 +#include <lemon/config.h>
    1.18 +
    1.19 +#ifdef LEMON_WIN32
    1.20  #include <lemon/bits/windows.h>
    1.21  #else
    1.22  #include <unistd.h>
    1.23 @@ -34,6 +36,7 @@
    1.24  #include <string>
    1.25  #include <fstream>
    1.26  #include <iostream>
    1.27 +#include <lemon/math.h>
    1.28  
    1.29  namespace lemon {
    1.30  
    1.31 @@ -63,16 +66,45 @@
    1.32      double cstime;
    1.33      double rtime;
    1.34  
    1.35 +  public:
    1.36 +    ///Display format specifier
    1.37 +
    1.38 +    ///\e
    1.39 +    ///
    1.40 +    enum Format {
    1.41 +      /// Reports all measured values
    1.42 +      NORMAL = 0,
    1.43 +      /// Only real time and an error indicator is displayed
    1.44 +      SHORT = 1
    1.45 +    };
    1.46 +
    1.47 +  private:
    1.48 +    static Format _format;
    1.49 +
    1.50      void _reset() {
    1.51        utime = stime = cutime = cstime = rtime = 0;
    1.52      }
    1.53  
    1.54    public:
    1.55  
    1.56 +    ///Set output format
    1.57 +
    1.58 +    ///Set output format.
    1.59 +    ///
    1.60 +    ///The output format is global for all timestamp instances.
    1.61 +    static void format(Format f) { _format = f; }
    1.62 +    ///Retrieve the current output format
    1.63 +
    1.64 +    ///Retrieve the current output format
    1.65 +    ///
    1.66 +    ///The output format is global for all timestamp instances.
    1.67 +    static Format format() { return _format; }
    1.68 +
    1.69 +
    1.70      ///Read the current time values of the process
    1.71      void stamp()
    1.72      {
    1.73 -#ifndef WIN32
    1.74 +#ifndef LEMON_WIN32
    1.75        timeval tv;
    1.76        gettimeofday(&tv, 0);
    1.77        rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
    1.78 @@ -224,11 +256,24 @@
    1.79    /// calculated.
    1.80    inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
    1.81    {
    1.82 -    os << "u: " << t.userTime() <<
    1.83 -      "s, s: " << t.systemTime() <<
    1.84 -      "s, cu: " << t.cUserTime() <<
    1.85 -      "s, cs: " << t.cSystemTime() <<
    1.86 -      "s, real: " << t.realTime() << "s";
    1.87 +    switch(t._format)
    1.88 +      {
    1.89 +      case TimeStamp::NORMAL:
    1.90 +        os << "u: " << t.userTime() <<
    1.91 +          "s, s: " << t.systemTime() <<
    1.92 +          "s, cu: " << t.cUserTime() <<
    1.93 +          "s, cs: " << t.cSystemTime() <<
    1.94 +          "s, real: " << t.realTime() << "s";
    1.95 +        break;
    1.96 +      case TimeStamp::SHORT:
    1.97 +        double total = t.userTime()+t.systemTime()+
    1.98 +          t.cUserTime()+t.cSystemTime();
    1.99 +        os << t.realTime()
   1.100 +           << "s (err: " << round((t.realTime()-total)/
   1.101 +                                  t.realTime()*10000)/100
   1.102 +           << "%)";
   1.103 +        break;
   1.104 +      }
   1.105      return os;
   1.106    }
   1.107  
   1.108 @@ -468,6 +513,7 @@
   1.109    {
   1.110      std::string _title;
   1.111      std::ostream &_os;
   1.112 +    bool _active;
   1.113    public:
   1.114      ///Constructor
   1.115  
   1.116 @@ -475,13 +521,27 @@
   1.117      ///\param title This text will be printed before the ellapsed time.
   1.118      ///\param os The stream to print the report to.
   1.119      ///\param run Sets whether the timer should start immediately.
   1.120 -    TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
   1.121 -      : Timer(run), _title(title), _os(os){}
   1.122 +    ///\param active Sets whether the report should actually be printed
   1.123 +    ///       on destruction.
   1.124 +    TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true,
   1.125 +               bool active=true)
   1.126 +      : Timer(run), _title(title), _os(os), _active(active) {}
   1.127      ///Destructor that prints the ellapsed time
   1.128      ~TimeReport()
   1.129      {
   1.130 -      _os << _title << *this << std::endl;
   1.131 +      if(_active) _os << _title << *this << std::endl;
   1.132      }
   1.133 +
   1.134 +    ///Retrieve the activity status
   1.135 +
   1.136 +    ///\e
   1.137 +    ///
   1.138 +    bool active() const { return _active; }
   1.139 +    ///Set the activity status
   1.140 +
   1.141 +    /// This function set whether the time report should actually be printed
   1.142 +    /// on destruction.
   1.143 +    void active(bool a) { _active=a; }
   1.144    };
   1.145  
   1.146    ///'Do nothing' version of TimeReport