Simpact Cyan
Population based event driven simulation using mNRM
debugtimer.h
1 #ifndef DEBUGTIMER_H
2 
3 #define DEBUGTIMER_H
4 
5 #ifndef NODEBUGTIMER
6 
7 #include <stdint.h>
8 #include <string>
9 #include <map>
10 #include <chrono>
11 
12 class DebugTimer;
13 
14 // Helper class to have a destructor to write timer info
15 class DebugTimerMap
16 {
17 public:
18  DebugTimerMap();
19  ~DebugTimerMap();
20 
21  std::map<std::string, DebugTimer *> m_timers;
22 };
23 
24 class DebugTimer
25 {
26 public:
27  ~DebugTimer();
28 
29  void start();
30  void stop();
31 
32  std::chrono::high_resolution_clock::duration getDuration() const { return m_totalDuration; }
33  int64_t getIterations() const { return m_count; }
34 
35  static DebugTimer *getTimer(const std::string &name);
36 private:
37  static DebugTimerMap m_timerMap;
38 
39  DebugTimer();
40 
41  uint64_t m_count;
42  std::chrono::high_resolution_clock::duration m_totalDuration;
43  std::chrono::time_point<std::chrono::high_resolution_clock> m_startTime;
44 };
45 
46 inline DebugTimer::DebugTimer() : m_count(0), m_totalDuration(0)
47 {
48 }
49 
50 inline DebugTimer::~DebugTimer()
51 {
52 }
53 
54 inline void DebugTimer::start()
55 {
56  m_startTime = std::chrono::high_resolution_clock::now();
57 }
58 
59 inline void DebugTimer::stop()
60 {
61  auto endTime = std::chrono::high_resolution_clock::now();
62  m_totalDuration += endTime-m_startTime;
63  m_count++;
64 }
65 
66 inline DebugTimer *DebugTimer::getTimer(const std::string &name)
67 {
68  auto it = m_timerMap.m_timers.find(name);
69  DebugTimer *pTimer = 0;
70 
71  if (it == m_timerMap.m_timers.end())
72  {
73  pTimer = new DebugTimer();
74  m_timerMap.m_timers[name] = pTimer;
75  }
76  else
77  pTimer = it->second;
78 
79  return pTimer;
80 }
81 
82 #endif // NODEBUGTIMER
83 
84 #endif // DEBUGTIMER_H