lemon/time_measure.h
changeset 1250 97d978243703
parent 833 e20173729589
child 1270 dceba191c00d
equal deleted inserted replaced
19:b7c37a060515 21:16db4cd5cf69
    32 #endif
    32 #endif
    33 
    33 
    34 #include <string>
    34 #include <string>
    35 #include <fstream>
    35 #include <fstream>
    36 #include <iostream>
    36 #include <iostream>
       
    37 #include <lemon/math.h>
    37 
    38 
    38 namespace lemon {
    39 namespace lemon {
    39 
    40 
    40   /// \addtogroup timecount
    41   /// \addtogroup timecount
    41   /// @{
    42   /// @{
    61     double stime;
    62     double stime;
    62     double cutime;
    63     double cutime;
    63     double cstime;
    64     double cstime;
    64     double rtime;
    65     double rtime;
    65 
    66 
       
    67   public:
       
    68     ///Display format specifier
       
    69 
       
    70     ///\e
       
    71     ///
       
    72     enum Format {
       
    73       /// Reports all measured values
       
    74       NORMAL = 0,
       
    75       /// Only real time and an error indicator is displayed
       
    76       SHORT = 1
       
    77     };
       
    78 
       
    79   private:
       
    80     static Format _format;
       
    81 
    66     void _reset() {
    82     void _reset() {
    67       utime = stime = cutime = cstime = rtime = 0;
    83       utime = stime = cutime = cstime = rtime = 0;
    68     }
    84     }
    69 
    85 
    70   public:
    86   public:
    71 
    87 
       
    88     ///Set output format
       
    89 
       
    90     ///Set output format.
       
    91     ///
       
    92     ///The output format is global for all timestamp instances.
       
    93     static void format(Format f) { _format = f; }
       
    94     ///Retrieve the current output format
       
    95 
       
    96     ///Retrieve the current output format
       
    97     ///
       
    98     ///The output format is global for all timestamp instances.
       
    99     static Format format() { return _format; }
       
   100 
       
   101     
    72     ///Read the current time values of the process
   102     ///Read the current time values of the process
    73     void stamp()
   103     void stamp()
    74     {
   104     {
    75 #ifndef WIN32
   105 #ifndef WIN32
    76       timeval tv;
   106       timeval tv;
   222   /// \relates TimeStamp
   252   /// \relates TimeStamp
   223   /// \note On <tt>WIN32</tt> platform the cummulative values are not
   253   /// \note On <tt>WIN32</tt> platform the cummulative values are not
   224   /// calculated.
   254   /// calculated.
   225   inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
   255   inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
   226   {
   256   {
   227     os << "u: " << t.userTime() <<
   257     switch(t._format)
   228       "s, s: " << t.systemTime() <<
   258       {
   229       "s, cu: " << t.cUserTime() <<
   259       case TimeStamp::NORMAL:
   230       "s, cs: " << t.cSystemTime() <<
   260         os << "u: " << t.userTime() <<
   231       "s, real: " << t.realTime() << "s";
   261           "s, s: " << t.systemTime() <<
       
   262           "s, cu: " << t.cUserTime() <<
       
   263           "s, cs: " << t.cSystemTime() <<
       
   264           "s, real: " << t.realTime() << "s";
       
   265         break;
       
   266       case TimeStamp::SHORT:
       
   267         double total = t.userTime()+t.systemTime()+
       
   268           t.cUserTime()+t.cSystemTime();
       
   269         os << t.realTime()
       
   270            << "s (err: " << round((t.realTime()-total)/
       
   271                                   t.realTime()*10000)/100
       
   272            << "%)";
       
   273         break;
       
   274       }
   232     return os;
   275     return os;
   233   }
   276   }
   234 
   277 
   235   ///Class for measuring the cpu time and real time usage of the process
   278   ///Class for measuring the cpu time and real time usage of the process
   236 
   279 
   466   ///\sa NoTimeReport
   509   ///\sa NoTimeReport
   467   class TimeReport : public Timer
   510   class TimeReport : public Timer
   468   {
   511   {
   469     std::string _title;
   512     std::string _title;
   470     std::ostream &_os;
   513     std::ostream &_os;
       
   514     bool _active;
   471   public:
   515   public:
   472     ///Constructor
   516     ///Constructor
   473 
   517 
   474     ///Constructor.
   518     ///Constructor.
   475     ///\param title This text will be printed before the ellapsed time.
   519     ///\param title This text will be printed before the ellapsed time.
   476     ///\param os The stream to print the report to.
   520     ///\param os The stream to print the report to.
   477     ///\param run Sets whether the timer should start immediately.
   521     ///\param run Sets whether the timer should start immediately.
   478     TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
   522     ///\param active Sets whether the report should actually be printed
   479       : Timer(run), _title(title), _os(os){}
   523     ///       on destruction.
       
   524     TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true,
       
   525 	       bool active=true)
       
   526       : Timer(run), _title(title), _os(os), _active(active) {}
   480     ///Destructor that prints the ellapsed time
   527     ///Destructor that prints the ellapsed time
   481     ~TimeReport()
   528     ~TimeReport()
   482     {
   529     {
   483       _os << _title << *this << std::endl;
   530       if(_active) _os << _title << *this << std::endl;
   484     }
   531     }
       
   532     
       
   533     ///Retrieve the activity status
       
   534 
       
   535     ///\e
       
   536     ///
       
   537     bool active() const { return _active; }
       
   538     ///Set the activity status
       
   539 
       
   540     /// This function set whether the time report should actually be printed
       
   541     /// on destruction.
       
   542     void active(bool a) { _active=a; }
   485   };
   543   };
   486 
   544 
   487   ///'Do nothing' version of TimeReport
   545   ///'Do nothing' version of TimeReport
   488 
   546 
   489   ///\sa TimeReport
   547   ///\sa TimeReport