Ignore:
Timestamp:
Apr 2, 2011, 12:20:10 AM (15 years ago)
Author:
Frederik Heber <heber@…>
Children:
bbd746
Parents:
93abe8
git-author:
Frederik Heber <heber@…> (03/15/11 09:53:34)
git-committer:
Frederik Heber <heber@…> (04/02/11 00:20:10)
Message:

Chronos is so far fully working.

  • Library version is now 7:0:0, API version is 1.1.0.
  • using either time.h or sys/times.h if present to measure timing.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Helpers/Chronos.cpp

    r93abe8 r8e24ef  
    2222#include <iostream>
    2323
     24#ifdef HAVE_SYS_TIMES_H
     25# include <sys/times.h>
     26#else
     27# include <time.h>
     28#endif
     29
    2430#include "Chronos.hpp"
    2531
     
    3238{}
    3339
    34 int Chronos::getTime(const std::string _name) const
     40double Chronos::getTime(const std::string _name) const
    3541{
    3642  // only those functions have a time that have run already
    37   if (IsTimeRunning.count(_name)) {
     43  if (TimeRunning.count(_name)) {
    3844    // return -1 if function is currently running
    39     if (!IsTimeRunning.count(_name))
     45    if (TimeRunning.count(_name) != 0.)
    4046      return AccountedTime.at(_name);
    4147    else
    42       return -1;
     48      return -1.;
    4349  }
    44   return 0;
     50  return 0.;
    4551}
    4652
    4753void Chronos::resetTime(const std::string _name)
    4854{
    49   if (IsTimeRunning.count(_name)) {
    50     AccountedTime[_name] = 0;
     55  if (TimeRunning.count(_name)) {
     56    AccountedTime[_name] = 0.;
    5157  }
    5258}
     
    5561{
    5662  // start time keeping
    57   IsTimeRunning[_name] = true;
     63  TimeRunning[_name] = getCurrentTime();
     64}
     65
     66double Chronos::getCurrentTime() const
     67{
     68#ifdef HAVE_SYS_TIMES_H
     69  struct tms *buffer = new tms;
     70  double currenttime;
     71  if (times(buffer) != (clock_t)(-1))
     72    currenttime = ((double)buffer->tms_utime/(double)CLOCKS_PER_SEC);
     73  else
     74    currenttime = 0.;
     75  delete buffer;
     76#else
     77  const double currenttime = (clock()/(double)CLOCKS_PER_SEC);
     78#endif
     79  //std::cout << "Current time is " << currenttime << std::endl;
     80  return currenttime;
    5881}
    5982
    6083void Chronos::endTiming(const std::string _name)
    6184{
    62   // finish time keeping if present
    63   ASSERT(IsTimeRunning.count(_name), "Chronos::endTiming() - no timer under "+_name+" running.");
    64   IsTimeRunning[_name] = false;
     85  const double endtime = getCurrentTime();
     86  const double starttime = TimeRunning[_name];
     87
     88  // if present
     89  ASSERT(TimeRunning.count(_name), "Chronos::endTiming() - no timer under "+_name+" running.");
     90  // finish time keeping
     91  const double RunTime = ((double)endtime - starttime);
     92  TimekeepingMap::iterator iter = AccountedTime.find(_name);
     93  if (iter != AccountedTime.end())
     94    AccountedTime[_name] += RunTime;
     95  else
     96    AccountedTime[_name] = RunTime;
     97
     98  // and zero for next run
     99  TimeRunning[_name] = 0.;
    65100}
    66101
    67 int Chronos::SumUpTotalTime() const
     102double Chronos::SumUpTotalTime() const
    68103{
    69   int sum = 0;
     104  double sum = 0.;
    70105  for (TimekeepingMap::const_iterator iter = AccountedTime.begin();
    71106      iter != AccountedTime.end();
     
    78113size_t Chronos::SumUpTotalFunctions() const
    79114{
    80   return IsTimeRunning.size();
     115  return TimeRunning.size();
    81116}
    82117
    83118std::ostream& operator<<(std::ostream &ost, const Chronos &_time)
    84119{
    85   int sum = _time.SumUpTotalTime();
    86   ost << "Total time passed: " << sum << std::endl;
     120  ost << "List of functions present:" << std::endl;
     121  for (Chronos::TimekeepingMap::const_iterator iter = _time.AccountedTime.begin();
     122      iter != _time.AccountedTime.end();
     123      ++iter)
     124    ost << "\t" << iter->first << "\t" << iter->second << "s" << std::endl;
     125  ost << "Total time passed: " << _time.SumUpTotalTime() << std::endl;
    87126  ost << "Total functions: " << _time.SumUpTotalFunctions() << std::endl;
    88 
    89127  return ost;
    90128}
    91129
     130// construct the remainder of the singleton
     131CONSTRUCT_SINGLETON(Chronos)
    92132
    93 CONSTRUCT_SINGLETON(Chronos)
     133// catch if someone wants to use Info objects in here
     134#ifdef INFO_HPP_
     135BOOST_PP_ASSERT_MSG(1,\
     136  ERROR: This is a safety measure to generate a compiler warning\n \
     137  if you really try to use info.hpp in __FILE__.)
     138#endif
     139
Note: See TracChangeset for help on using the changeset viewer.