Changeset 8e24ef


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.
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • configure.ac

    r93abe8 r8e24ef  
    33
    44AC_PREREQ([2.65])
    5 AC_INIT([CodePatterns], [1.0.13], [heber@ins.uni-bonn.de], [codepatterns], [http://trac.ins.uni-bonn.de/projects/CodePatterns/])
     5AC_INIT([CodePatterns], [1.1.0], [heber@ins.uni-bonn.de], [codepatterns], [http://trac.ins.uni-bonn.de/projects/CodePatterns/])
    66AC_CONFIG_AUX_DIR(config)
    77AC_CONFIG_SRCDIR([src/Patterns/Singleton_impl.hpp])
     
    2525# refer to the libtool manual, section "Updating library version information":
    2626# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
    27 AC_SUBST([CODEPATTERNS_SO_VERSION], [6:0:1])
    28 AC_SUBST([CODEPATTERNS_API_VERSION], [1.0.13])
     27AC_SUBST([CODEPATTERNS_SO_VERSION], [7:0:0])
     28AC_SUBST([CODEPATTERNS_API_VERSION], [1.1.0])
    2929
    3030# Checks for libraries.
     
    4141AC_C_INLINE
    4242AC_TYPE_SIZE_T
     43AC_CHECK_HEADERS([sys/times.h])
    4344
    4445# Checks for library functions.
  • 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
  • src/Helpers/Chronos.hpp

    r93abe8 r8e24ef  
    2626  //!> unit tests needs access to private members
    2727  friend class ChronosTest;
     28  //!> operator needs to access some internals
     29  friend std::ostream& operator<<(std::ostream &ost, const Chronos &_time);
    2830public :
    2931
     
    3335   * @return current amount of time passed for this function, 0 if unknown, -1 if currently running
    3436   */
    35   int getTime(const std::string _name) const;
     37  double getTime(const std::string _name) const;
    3638
    3739  /** Resets time counter for this function \a _name to zero.
     
    5557  /** Sums up total time accounted for.
    5658   *
    57    * @return total time
     59   * @return total time in seconds
    5860   */
    59   int SumUpTotalTime() const;
     61  double SumUpTotalTime() const;
    6062
    6163  /** Sums up all functions accounted.
     
    7274
    7375private:
     76  /** Returns the current timer in seconds.
     77   *
     78   * This function is present to allow for changing of time measurements
     79   * without the other functions noticing.
     80   *
     81   * \note We convert clock() by CLOCKS_PER_SEC.
     82   *
     83   * @return current time in unit of seconds
     84   */
     85  double getCurrentTime() const;
    7486
    75   typedef std::map<const std::string, int> TimekeepingMap;
    76   typedef std::map<const std::string, bool> TimerStatusMap;
     87
     88  typedef std::map<const std::string, double> TimekeepingMap;
     89  typedef std::map<const std::string, double> TimerStatusMap;
    7790
    7891  TimekeepingMap AccountedTime;
    79   TimerStatusMap IsTimeRunning;
     92  TimerStatusMap TimeRunning;
    8093};
    8194
  • src/Helpers/Info.cpp

    r93abe8 r8e24ef  
    3939{
    4040  verbosity++;
    41   DoLog(0) && (Log() << Verbose(0) << "Begin of " << FunctionName << endl);
     41  LOG(0, "INFO: Begin of "+FunctionName);
    4242
    4343  // one more info object
     
    6262
    6363  // verbosity stuff
    64   DoLog(0) && (Log() << Verbose(0) << "End of " << FunctionName << endl);
     64  LOG(0, "INFO: End of "+FunctionName);
    6565  verbosity--;
    6666
  • src/unittests/ChronosUnitTest.cpp

    r93abe8 r8e24ef  
    4040{
    4141  Info FunctionInfo(__func__);
     42  for (int i=0;i<10000000;++i)
     43    std::cout << "";
    4244}
    4345
     
    4547{
    4648  Info FunctionInfo(__func__);
     49  for (int i=0;i<1000000;++i)
     50    std::cout << "";
    4751}
    4852
     
    8084void ChronosTest::dummyTest()
    8185{
     86  double timings[4];
    8287  // first dummy
    8388  dummy();
    84   CPPUNIT_ASSERT( Chronos::getInstance().IsTimeRunning.find(std::string("dummy"))
    85       != Chronos::getInstance().IsTimeRunning.end() );
     89  CPPUNIT_ASSERT( Chronos::getInstance().TimeRunning.find(std::string("dummy"))
     90      != Chronos::getInstance().TimeRunning.end() );
    8691  CPPUNIT_ASSERT_EQUAL( (size_t) 1, Chronos::getInstance().SumUpTotalFunctions() );
     92  timings[0] = Chronos::getInstance().AccountedTime[std::string("dummy")];
     93  std::cout << "Timing[0]: " << timings[0] << std::endl;
     94  CPPUNIT_ASSERT(timings[0] > 0.);
    8795  dummy();
    8896  // second call goes to same entry
    8997  CPPUNIT_ASSERT_EQUAL( (size_t) 1, Chronos::getInstance().SumUpTotalFunctions() );
     98  timings[1] = Chronos::getInstance().AccountedTime[std::string("dummy")] - timings[0];
     99  std::cout << "Timing[1]: " << timings[1] << std::endl;
     100  CPPUNIT_ASSERT(timings[1] > 0.);
     101
    90102
    91103  // second dummy
    92104  dummy_two();
    93   CPPUNIT_ASSERT( Chronos::getInstance().IsTimeRunning.find(std::string("dummy_two"))
    94       != Chronos::getInstance().IsTimeRunning.end() );
     105  CPPUNIT_ASSERT( Chronos::getInstance().TimeRunning.find(std::string("dummy_two"))
     106      != Chronos::getInstance().TimeRunning.end() );
    95107  CPPUNIT_ASSERT_EQUAL( (size_t) 2, Chronos::getInstance().SumUpTotalFunctions() );
     108  timings[2] = Chronos::getInstance().AccountedTime[std::string("dummy_two")];
     109  std::cout << "Timing[2]: " << timings[2] << std::endl;
     110  CPPUNIT_ASSERT(timings[2] > 0.);
     111
     112  // "inline" dummy
     113  {
     114    Info DummyInfo("dummy_three");
     115    for (int i=0;i<1000000;++i)
     116      std::cout << "";
     117  }
     118  CPPUNIT_ASSERT( Chronos::getInstance().TimeRunning.find(std::string("dummy_three"))
     119      != Chronos::getInstance().TimeRunning.end() );
     120  CPPUNIT_ASSERT_EQUAL( (size_t) 3, Chronos::getInstance().SumUpTotalFunctions() );
     121  timings[3] = Chronos::getInstance().AccountedTime[std::string("dummy_three")];
     122  std::cout << "Timing[3]: " << timings[3] << std::endl;
     123  CPPUNIT_ASSERT(timings[3] > 0.);
     124
     125  // check summing of times
     126  CPPUNIT_ASSERT_EQUAL( timings[0] + timings[1] + timings[2] + timings[3], Chronos::getInstance().SumUpTotalTime());
     127
     128  std::cout << Chronos::getInstance() << std::endl;
    96129}
Note: See TracChangeset for help on using the changeset viewer.