diff -r cd72eae05bdf -r 3c00344f49c9 lemon/time_measure.h --- a/lemon/time_measure.h Mon Jul 16 16:21:40 2018 +0200 +++ b/lemon/time_measure.h Wed Oct 17 19:14:07 2018 +0200 @@ -2,7 +2,7 @@ * * This file is a part of LEMON, a generic C++ optimization library. * - * Copyright (C) 2003-2009 + * Copyright (C) 2003-2013 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Research Group on Combinatorial Optimization, EGRES). * @@ -23,7 +23,9 @@ ///\file ///\brief Tools for measuring cpu usage -#ifdef WIN32 +#include + +#ifdef LEMON_WIN32 #include #else #include @@ -34,6 +36,7 @@ #include #include #include +#include namespace lemon { @@ -63,16 +66,45 @@ double cstime; double rtime; + public: + ///Display format specifier + + ///\e + /// + enum Format { + /// Reports all measured values + NORMAL = 0, + /// Only real time and an error indicator is displayed + SHORT = 1 + }; + + private: + static Format _format; + void _reset() { utime = stime = cutime = cstime = rtime = 0; } public: + ///Set output format + + ///Set output format. + /// + ///The output format is global for all timestamp instances. + static void format(Format f) { _format = f; } + ///Retrieve the current output format + + ///Retrieve the current output format + /// + ///The output format is global for all timestamp instances. + static Format format() { return _format; } + + ///Read the current time values of the process void stamp() { -#ifndef WIN32 +#ifndef LEMON_WIN32 timeval tv; gettimeofday(&tv, 0); rtime=tv.tv_sec+double(tv.tv_usec)/1e6; @@ -224,11 +256,24 @@ /// calculated. inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) { - os << "u: " << t.userTime() << - "s, s: " << t.systemTime() << - "s, cu: " << t.cUserTime() << - "s, cs: " << t.cSystemTime() << - "s, real: " << t.realTime() << "s"; + switch(t._format) + { + case TimeStamp::NORMAL: + os << "u: " << t.userTime() << + "s, s: " << t.systemTime() << + "s, cu: " << t.cUserTime() << + "s, cs: " << t.cSystemTime() << + "s, real: " << t.realTime() << "s"; + break; + case TimeStamp::SHORT: + double total = t.userTime()+t.systemTime()+ + t.cUserTime()+t.cSystemTime(); + os << t.realTime() + << "s (err: " << round((t.realTime()-total)/ + t.realTime()*10000)/100 + << "%)"; + break; + } return os; } @@ -468,6 +513,7 @@ { std::string _title; std::ostream &_os; + bool _active; public: ///Constructor @@ -475,13 +521,27 @@ ///\param title This text will be printed before the ellapsed time. ///\param os The stream to print the report to. ///\param run Sets whether the timer should start immediately. - TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) - : Timer(run), _title(title), _os(os){} + ///\param active Sets whether the report should actually be printed + /// on destruction. + TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true, + bool active=true) + : Timer(run), _title(title), _os(os), _active(active) {} ///Destructor that prints the ellapsed time ~TimeReport() { - _os << _title << *this << std::endl; + if(_active) _os << _title << *this << std::endl; } + + ///Retrieve the activity status + + ///\e + /// + bool active() const { return _active; } + ///Set the activity status + + /// This function set whether the time report should actually be printed + /// on destruction. + void active(bool a) { _active=a; } }; ///'Do nothing' version of TimeReport