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