TimingProfile.h
1/* Distributed under the Apache License, Version 2.0.
2 See accompanying NOTICE file for details.*/
3
4#pragma once
5
6#include "cdm/utils/Logger.h"
7
8#include <chrono>
9
10class CDM_DECL TimingProfile
11{
12 using Clock = std::chrono::high_resolution_clock;
13public:
15 virtual ~TimingProfile() {}
16
17 enum class State
18 {
19 Ready, // Timer has no data and is not running
20 Running, // Timer is currently running
21 Ran // Timer has been stopped and has data
22 };
23
24 struct Timer
25 {
26 Clock::time_point start;
27 Clock::time_point end;
28 State state = State::Ready;
29 };
30
31 void Clear();
32
33 Timer& GetTimer(const std::string& label);
34
41 void Reset(Timer& timer);
42 void Reset(const std::string& label)
43 {
44 return Reset(GetTimer(label));
45 }
46
52 void Start(Timer& timer);
53 Timer& Start(const std::string& label)
54 {
55 Timer& timer = GetTimer(label);
56 Start(timer);
57 return timer;
58 }
59
63 void Stop(Timer& timer);
64 void Stop(const std::string& label)
65 {
66 return Stop(GetTimer(label));
67 }
68
72 Clock::duration GetDuration(Timer& timer);
73 Clock::duration GetDuration(const std::string& label)
74 {
75 return GetDuration(GetTimer(label));
76 }
77
83 double GetElapsedTime_s(Timer& timer);
84 double GetElapsedTime_s(const std::string& label)
85 {
86 return GetElapsedTime_s(GetTimer(label));
87 }
88
92 template<typename Duration>
93 typename Duration::rep GetElapsedTime(const Timer& timer)
94 {
95 if (timer.state == State::Running)
96 {
97 return std::chrono::duration_cast<Duration>(Clock::now() - timer.start).count();
98 }
99 else if (timer.state == State::Ran)
100 {
101 return std::chrono::duration_cast<Duration>(timer.end - timer.start).count();
102 }
103 else
104 {
105 return typename Duration::rep(0);
106 }
107 }
108 template<typename Duration>
109 typename Duration::rep GetElapsedTime(const std::string& label)
110 {
111 return GetElapsedTime<Duration>(GetTimer(label));
112 }
113
114private:
115 std::map<std::string, Timer> m_timers;
116};
Definition: TimingProfile.h:11
virtual ~TimingProfile()
Definition: TimingProfile.h:15
double GetElapsedTime_s(const std::string &label)
Definition: TimingProfile.h:84
void Reset(const std::string &label)
Definition: TimingProfile.h:42
Duration::rep GetElapsedTime(const Timer &timer)
Definition: TimingProfile.h:93
Clock::duration GetDuration(const std::string &label)
Definition: TimingProfile.h:73
std::chrono::high_resolution_clock Clock
Definition: TimingProfile.h:12
TimingProfile()
Definition: TimingProfile.h:14
Timer & Start(const std::string &label)
Definition: TimingProfile.h:53
State
Definition: TimingProfile.h:18
std::map< std::string, Timer > m_timers
Definition: TimingProfile.h:115
Duration::rep GetElapsedTime(const std::string &label)
Definition: TimingProfile.h:109
void Stop(const std::string &label)
Definition: TimingProfile.h:64
Definition: TimingProfile.h:25
State state
Definition: TimingProfile.h:28
Clock::time_point end
Definition: TimingProfile.h:27
Clock::time_point start
Definition: TimingProfile.h:26

Distributed under the Apache License, Version 2.0.

See accompanying NOTICE file for details.