lemon/time_measure.h
changeset 1222 c40a9d94442d
parent 833 e20173729589
child 1270 dceba191c00d
     1.1 --- a/lemon/time_measure.h	Sun May 06 17:18:39 2012 +0200
     1.2 +++ b/lemon/time_measure.h	Tue Jun 05 12:06:28 2012 +0200
     1.3 @@ -34,6 +34,7 @@
     1.4  #include <string>
     1.5  #include <fstream>
     1.6  #include <iostream>
     1.7 +#include <lemon/math.h>
     1.8  
     1.9  namespace lemon {
    1.10  
    1.11 @@ -63,12 +64,41 @@
    1.12      double cstime;
    1.13      double rtime;
    1.14  
    1.15 +  public:
    1.16 +    ///Display format specifier
    1.17 +
    1.18 +    ///\e
    1.19 +    ///
    1.20 +    enum Format {
    1.21 +      /// Reports all measured values
    1.22 +      NORMAL = 0,
    1.23 +      /// Only real time and an error indicator is displayed
    1.24 +      SHORT = 1
    1.25 +    };
    1.26 +
    1.27 +  private:
    1.28 +    static Format _format;
    1.29 +
    1.30      void _reset() {
    1.31        utime = stime = cutime = cstime = rtime = 0;
    1.32      }
    1.33  
    1.34    public:
    1.35  
    1.36 +    ///Set output format
    1.37 +
    1.38 +    ///Set output format.
    1.39 +    ///
    1.40 +    ///The output format is global for all timestamp instances.
    1.41 +    static void format(Format f) { _format = f; }
    1.42 +    ///Retrieve the current output format
    1.43 +
    1.44 +    ///Retrieve the current output format
    1.45 +    ///
    1.46 +    ///The output format is global for all timestamp instances.
    1.47 +    static Format format() { return _format; }
    1.48 +
    1.49 +    
    1.50      ///Read the current time values of the process
    1.51      void stamp()
    1.52      {
    1.53 @@ -224,11 +254,24 @@
    1.54    /// calculated.
    1.55    inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
    1.56    {
    1.57 -    os << "u: " << t.userTime() <<
    1.58 -      "s, s: " << t.systemTime() <<
    1.59 -      "s, cu: " << t.cUserTime() <<
    1.60 -      "s, cs: " << t.cSystemTime() <<
    1.61 -      "s, real: " << t.realTime() << "s";
    1.62 +    switch(t._format)
    1.63 +      {
    1.64 +      case TimeStamp::NORMAL:
    1.65 +        os << "u: " << t.userTime() <<
    1.66 +          "s, s: " << t.systemTime() <<
    1.67 +          "s, cu: " << t.cUserTime() <<
    1.68 +          "s, cs: " << t.cSystemTime() <<
    1.69 +          "s, real: " << t.realTime() << "s";
    1.70 +        break;
    1.71 +      case TimeStamp::SHORT:
    1.72 +        double total = t.userTime()+t.systemTime()+
    1.73 +          t.cUserTime()+t.cSystemTime();
    1.74 +        os << t.realTime()
    1.75 +           << "s (err: " << round((t.realTime()-total)/
    1.76 +                                  t.realTime()*10000)/100
    1.77 +           << "%)";
    1.78 +        break;
    1.79 +      }
    1.80      return os;
    1.81    }
    1.82  
    1.83 @@ -468,6 +511,7 @@
    1.84    {
    1.85      std::string _title;
    1.86      std::ostream &_os;
    1.87 +    bool _active;
    1.88    public:
    1.89      ///Constructor
    1.90  
    1.91 @@ -475,13 +519,27 @@
    1.92      ///\param title This text will be printed before the ellapsed time.
    1.93      ///\param os The stream to print the report to.
    1.94      ///\param run Sets whether the timer should start immediately.
    1.95 -    TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
    1.96 -      : Timer(run), _title(title), _os(os){}
    1.97 +    ///\param active Sets whether the report should actually be printed
    1.98 +    ///       on destruction.
    1.99 +    TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true,
   1.100 +	       bool active=true)
   1.101 +      : Timer(run), _title(title), _os(os), _active(active) {}
   1.102      ///Destructor that prints the ellapsed time
   1.103      ~TimeReport()
   1.104      {
   1.105 -      _os << _title << *this << std::endl;
   1.106 +      if(_active) _os << _title << *this << std::endl;
   1.107      }
   1.108 +    
   1.109 +    ///Retrieve the activity status
   1.110 +
   1.111 +    ///\e
   1.112 +    ///
   1.113 +    bool active() const { return _active; }
   1.114 +    ///Set the activity status
   1.115 +
   1.116 +    /// This function set whether the time report should actually be printed
   1.117 +    /// on destruction.
   1.118 +    void active(bool a) { _active=a; }
   1.119    };
   1.120  
   1.121    ///'Do nothing' version of TimeReport