source: src/CodePatterns/Chronos.hpp@ b9273a

Last change on this file since b9273a was 3f06bb, checked in by Frederik Heber <heber@…>, 13 years ago

Enhanced Chronos for more accurate timekeeping.

  • Chronos is now safe to use w.r.t. recursive function calls.
  • Chronos now gets const ref of token name in each function.
  • TESTFIX: dummyTest() now checks on recursive function.
  • Chronos now offers its internal timekeeping via const ref to outside.
  • Chronos also now has up to nanoseconds precision when either time.h or sys/time.h is present.
  • librt required from clock_gettime().
  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Chronos.hpp
3 *
4 * Created on: Mar 14, 2011
5 * Author: heber
6 */
7
8#ifndef CHRONOS_HPP_
9#define CHRONOS_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include <iosfwd>
17#include <map>
18#include <string>
19
20#include "CodePatterns/Singleton.hpp"
21
22class ChronosTest;
23
24class Chronos : public Singleton<Chronos>{
25 //!> Singleton pattern needs private access
26 friend class Singleton<Chronos>;
27 //!> unit tests needs access to private members
28 friend class ChronosTest;
29 //!> operator needs to access some internals
30 friend std::ostream& operator<<(std::ostream &ost, const Chronos &_time);
31public :
32 //!> typedef for the map storing times per token
33 typedef std::map<const std::string, double> TimekeepingMap;
34
35 /** Returns current kept time of function \a _name.
36 *
37 * @param _name name of function
38 * @return current amount of time passed for this function, 0 if unknown, -1 if currently running
39 */
40 double getTime(const std::string &_name) const;
41
42 /** Resets time counter for this function \a _name to zero.
43 *
44 * @param _name name of function
45 */
46 void resetTime(const std::string &_name);
47
48 /** Starts Timing for this function \a _name.
49 *
50 * @param _name name of function
51 */
52 void startTiming(const std::string &_name);
53
54 /** Finishes Timing for this function \a _name.
55 *
56 * @param _name name of function
57 */
58 void endTiming(const std::string &_name);
59
60 /** Returns const reference to time keeping map.
61 *
62 * \return const ref to timekeeping map
63 */
64 const TimekeepingMap& getTimekeepingMap() const;
65
66 /** Sums up total time accounted for.
67 *
68 * @return total time in seconds
69 */
70 double SumUpTotalTime() const;
71
72 /** Sums up all functions accounted.
73 *
74 * @return number of functions
75 */
76 size_t SumUpTotalFunctions() const;
77
78protected:
79 /** Do not call this constructor directly, use getInstance() instead. */
80 Chronos();
81 /** Do not call this destructor directly, use purgeInstance() instead. */
82 ~Chronos();
83
84private:
85 /** Returns the current timer in seconds.
86 *
87 * This function is present to allow for changing of time measurements
88 * without the other functions noticing.
89 *
90 * \note We convert clock() by CLOCKS_PER_SEC.
91 * \note We convert struct tms::tms_utime by _SC_CLK_TCK via sysconf().
92 *
93 * @return current time in unit of seconds
94 */
95 double getCurrentTime() const;
96
97 //!> typedef for the map storing status of time keeping per token
98 typedef std::map<const std::string, double> TimerMap;
99
100 //!> typedef for the map storing number of recursive calls to this token
101 typedef std::map<const std::string, size_t> TimerRecursionMap;
102
103 //!> map storing times per token
104 TimekeepingMap AccountedTime;
105 //!> map storing time keeping status per token
106 TimerMap StartingTime;
107 //!> map storing level of recursion per token
108 TimerRecursionMap RecursionMap;
109
110#ifdef HAVE_TIME_H
111 timespec basetime;
112#else
113#ifdef HAVE_SYS_TIME_H
114 struct timezone basetime;
115#else
116#ifdef HAVE_SYS_TIMES_H
117 struct tms *basetime;
118#endif
119#endif
120#endif
121};
122
123/** Output for Chronos.
124 *
125 * @param ost output stream
126 * @param _time Chronos to print
127 * @return reference to given output stream for concatenation
128 */
129std::ostream& operator<<(std::ostream &ost, const Chronos &_time);
130
131// inline functions
132
133inline
134const Chronos::TimekeepingMap& Chronos::getTimekeepingMap() const
135{
136 return AccountedTime;
137}
138
139#endif /* CHRONOS_HPP_ */
Note: See TracBrowser for help on using the repository browser.