COIN-OR::LEMON - Graph Library

Changeset 1780:9f052750753f in lemon-0.x


Ignore:
Timestamp:
11/08/05 11:12:45 (18 years ago)
Author:
Alpar Juttner
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2314
Message:
  • Timer can be stop()ed and (re)start()ed.
  • Obsolete \bug removed
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/time_measure.h

    r1689 r1780  
    6767    const rtms &getTms() const {return ts;}
    6868
     69    void _reset()
     70    { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
     71
    6972  public:
    7073
     
    8184    /// Constructor initializing with zero
    8285    TimeStamp()
    83     { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
     86    { _reset(); }
    8487    ///Constructor initializing with the current time values of the process
    8588    TimeStamp(void *) { stamp();}
    8689 
     90    ///Set every time value to zero
     91    TimeStamp &reset() {_reset();return *this;}
     92
    8793    ///\e
    8894    TimeStamp &operator+=(const TimeStamp &b)
     
    118124    }
    119125    ///\e
    120 
    121     ///\bug operator * and / gives rounded values!
    122126    TimeStamp &operator*=(double b)
    123127    {
     
    181185      return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
    182186    }
    183     ///Gives back the real time of the process
     187    ///Gives back the real time
    184188    double realTime() const {return real_time;}
    185189  };
     
    190194  }
    191195 
    192   ///Class measuring the cpu time and real time usage of the process
    193 
    194   ///Class measuring the cpu time and real time usage of the process.
     196  ///Class for measuring the cpu time and real time usage of the process
     197
     198  ///Class for measuring the cpu time and real time usage of the process.
    195199  ///It is quite easy-to-use, here is a short example.
    196200  ///\code
     
    215219  ///\endcode
    216220  ///
     221  ///The \ref Timer can also be \ref stop() "stopped" and
     222  ///\ref start() "started" again, so it is easy to compute collected
     223  ///running times.
     224  ///
     225  ///\warning Depending on the operation system and its actual configuration
     226  ///the time counters have a certain (relatively big) granularity.
     227  ///Therefore this tool is not appropriate to measure very short times.
     228  ///Also, if you start and stop the timer very frequently, it could lead
     229  ///distorted results.
     230  ///
     231  ///The \ref Timer also counts the number of \ref start()
     232  ///executions, and is stops only after the same amount (or more)
     233  ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
     234  ///of recursive functions.
     235  ///
    217236  ///\todo This shouldn't be Unix (Linux) specific.
    218237  ///
     
    220239  class Timer
    221240  {
    222     TimeStamp start_time;
    223 
    224     void _reset() {start_time.stamp();}
     241    int running; //Timer is running iff running>0; (running>=0 always holds)
     242    TimeStamp start_time; //This is the relativ start-time if the timer
     243                          //is running, the collected running time otherwise.
     244   
     245    void _reset() {if(running) start_time.stamp(); else start_time.reset();}
    225246 
    226247  public:
    227     ///Constructor. It starts with zero time counters
    228     Timer() {_reset();}
     248    ///Constructor.
     249
     250    ///\param _running indicates whether or not the timer starts immediately.
     251    ///
     252    Timer(bool _running=true) :running(_running) {_reset();}
    229253
    230254    ///Computes the ellapsed time
    231255
    232256    ///This conversion computes the ellapsed time
    233     ///since the construction of \c t or since
    234     ///the last \c t.reset().
     257    ///
    235258    operator TimeStamp () const
    236259    {
    237260      TimeStamp t;
    238261      t.stamp();
    239       return t-start_time;
     262      return running?t-start_time:start_time;
    240263    }
    241264
     
    249272    }
    250273
    251 
     274    ///Start the time counters
     275   
     276    ///This function starts the time counters.
     277    ///
     278    ///If the timer is started more than ones, it will remain running
     279    ///until the same amount of \ref stop() is called.
     280    ///\sa stop()
     281    void start()
     282    {
     283      if(running) running++;
     284      else {
     285        TimeStamp t;
     286        t.stamp();
     287        start_time=t-start_time;
     288      }
     289    }
     290   
     291    ///Stop the time counters
     292
     293    ///This function stops the time counters.
     294    ///
     295    ///\sa stop()
     296    void stop()
     297    {
     298      if(running && !--running) {
     299        TimeStamp t;
     300        t.stamp();
     301        start_time=t-start_time;
     302      }
     303    }
     304   
    252305    ///Gives back the ellapsed user time of the process
    253306    double userTime() const
     
    270323      return operator TimeStamp().cSystemTime();
    271324    }
    272     ///Gives back the ellapsed real time of the process
     325    ///Gives back the ellapsed real time
    273326    double realTime() const
    274327    {
     
    307360  ///This function calls \c f several times and returns the average
    308361  ///running time. The number of the executions will be choosen in such a way
    309   ///that the full running time will be roughly between \c min_time
     362  ///that the full real running time will be roughly between \c min_time
    310363  ///and <tt>2*min_time</tt>.
    311364  ///\param f the function object to be measured.
Note: See TracChangeset for help on using the changeset viewer.