New features in time_measure.h (#442)
authorAlpar Juttner <alpar@cs.elte.hu>
Tue, 05 Jun 2012 12:06:28 +0200
changeset 1054c40a9d94442d
parent 989 38e1d4383262
child 1055 05b34170866b
New features in time_measure.h (#442)
lemon/base.cc
lemon/math.h
lemon/time_measure.h
     1.1 --- a/lemon/base.cc	Sun May 06 17:18:39 2012 +0200
     1.2 +++ b/lemon/base.cc	Tue Jun 05 12:06:28 2012 +0200
     1.3 @@ -21,6 +21,7 @@
     1.4  
     1.5  #include<lemon/tolerance.h>
     1.6  #include<lemon/core.h>
     1.7 +#include<lemon/time_measure.h>
     1.8  namespace lemon {
     1.9  
    1.10    float Tolerance<float>::def_epsilon = static_cast<float>(1e-4);
    1.11 @@ -31,4 +32,6 @@
    1.12    const Invalid INVALID = Invalid();
    1.13  #endif
    1.14  
    1.15 +  TimeStamp::Format TimeStamp::_format = TimeStamp::NORMAL;
    1.16 +
    1.17  } //namespace lemon
     2.1 --- a/lemon/math.h	Sun May 06 17:18:39 2012 +0200
     2.2 +++ b/lemon/math.h	Tue Jun 05 12:06:28 2012 +0200
     2.3 @@ -65,8 +65,13 @@
     2.4        return v!=v;
     2.5      }
     2.6  
     2.7 +  ///Round a value to its closest integer
     2.8 +  inline double round(double r) {
     2.9 +    return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
    2.10 +  }
    2.11 +
    2.12    /// @}
    2.13  
    2.14  } //namespace lemon
    2.15  
    2.16 -#endif //LEMON_TOLERANCE_H
    2.17 +#endif //LEMON_MATH_H
     3.1 --- a/lemon/time_measure.h	Sun May 06 17:18:39 2012 +0200
     3.2 +++ b/lemon/time_measure.h	Tue Jun 05 12:06:28 2012 +0200
     3.3 @@ -34,6 +34,7 @@
     3.4  #include <string>
     3.5  #include <fstream>
     3.6  #include <iostream>
     3.7 +#include <lemon/math.h>
     3.8  
     3.9  namespace lemon {
    3.10  
    3.11 @@ -63,12 +64,41 @@
    3.12      double cstime;
    3.13      double rtime;
    3.14  
    3.15 +  public:
    3.16 +    ///Display format specifier
    3.17 +
    3.18 +    ///\e
    3.19 +    ///
    3.20 +    enum Format {
    3.21 +      /// Reports all measured values
    3.22 +      NORMAL = 0,
    3.23 +      /// Only real time and an error indicator is displayed
    3.24 +      SHORT = 1
    3.25 +    };
    3.26 +
    3.27 +  private:
    3.28 +    static Format _format;
    3.29 +
    3.30      void _reset() {
    3.31        utime = stime = cutime = cstime = rtime = 0;
    3.32      }
    3.33  
    3.34    public:
    3.35  
    3.36 +    ///Set output format
    3.37 +
    3.38 +    ///Set output format.
    3.39 +    ///
    3.40 +    ///The output format is global for all timestamp instances.
    3.41 +    static void format(Format f) { _format = f; }
    3.42 +    ///Retrieve the current output format
    3.43 +
    3.44 +    ///Retrieve the current output format
    3.45 +    ///
    3.46 +    ///The output format is global for all timestamp instances.
    3.47 +    static Format format() { return _format; }
    3.48 +
    3.49 +    
    3.50      ///Read the current time values of the process
    3.51      void stamp()
    3.52      {
    3.53 @@ -224,11 +254,24 @@
    3.54    /// calculated.
    3.55    inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
    3.56    {
    3.57 -    os << "u: " << t.userTime() <<
    3.58 -      "s, s: " << t.systemTime() <<
    3.59 -      "s, cu: " << t.cUserTime() <<
    3.60 -      "s, cs: " << t.cSystemTime() <<
    3.61 -      "s, real: " << t.realTime() << "s";
    3.62 +    switch(t._format)
    3.63 +      {
    3.64 +      case TimeStamp::NORMAL:
    3.65 +        os << "u: " << t.userTime() <<
    3.66 +          "s, s: " << t.systemTime() <<
    3.67 +          "s, cu: " << t.cUserTime() <<
    3.68 +          "s, cs: " << t.cSystemTime() <<
    3.69 +          "s, real: " << t.realTime() << "s";
    3.70 +        break;
    3.71 +      case TimeStamp::SHORT:
    3.72 +        double total = t.userTime()+t.systemTime()+
    3.73 +          t.cUserTime()+t.cSystemTime();
    3.74 +        os << t.realTime()
    3.75 +           << "s (err: " << round((t.realTime()-total)/
    3.76 +                                  t.realTime()*10000)/100
    3.77 +           << "%)";
    3.78 +        break;
    3.79 +      }
    3.80      return os;
    3.81    }
    3.82  
    3.83 @@ -468,6 +511,7 @@
    3.84    {
    3.85      std::string _title;
    3.86      std::ostream &_os;
    3.87 +    bool _active;
    3.88    public:
    3.89      ///Constructor
    3.90  
    3.91 @@ -475,13 +519,27 @@
    3.92      ///\param title This text will be printed before the ellapsed time.
    3.93      ///\param os The stream to print the report to.
    3.94      ///\param run Sets whether the timer should start immediately.
    3.95 -    TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
    3.96 -      : Timer(run), _title(title), _os(os){}
    3.97 +    ///\param active Sets whether the report should actually be printed
    3.98 +    ///       on destruction.
    3.99 +    TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true,
   3.100 +	       bool active=true)
   3.101 +      : Timer(run), _title(title), _os(os), _active(active) {}
   3.102      ///Destructor that prints the ellapsed time
   3.103      ~TimeReport()
   3.104      {
   3.105 -      _os << _title << *this << std::endl;
   3.106 +      if(_active) _os << _title << *this << std::endl;
   3.107      }
   3.108 +    
   3.109 +    ///Retrieve the activity status
   3.110 +
   3.111 +    ///\e
   3.112 +    ///
   3.113 +    bool active() const { return _active; }
   3.114 +    ///Set the activity status
   3.115 +
   3.116 +    /// This function set whether the time report should actually be printed
   3.117 +    /// on destruction.
   3.118 +    void active(bool a) { _active=a; }
   3.119    };
   3.120  
   3.121    ///'Do nothing' version of TimeReport