Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1#pragma once
2
3#include <Common/primitive_type.h>
4
5#include <chrono>
6#include <ratio>
7
8namespace ph
9{
10
13class Timer final
14{
15public:
16 using Clock = std::chrono::steady_clock;
17 using DefaultTimeUnit = Clock::duration;
18
19public:
20 Timer();
21
24 Timer& start();
25
30
33 Timer& stop();
34
40
46
51
58 template<typename NumberType = uint64>
59 NumberType markLapS();
60
61 template<typename NumberType = uint64>
62 NumberType markLapMs();
63
64 template<typename NumberType = uint64>
65 NumberType markLapUs();
66
67 template<typename NumberType = uint64>
68 NumberType markLapNs();
69
70 template<typename NumberType = uint64>
71 NumberType getDeltaS() const;
72
73 template<typename NumberType = uint64>
74 NumberType getDeltaMs() const;
75
76 template<typename NumberType = uint64>
77 NumberType getDeltaUs() const;
78
79 template<typename NumberType = uint64>
80 NumberType getDeltaNs() const;
82
83private:
84 template<typename TimeRepresentation, typename TimePeriod>
85 static TimeRepresentation castToNumber(DefaultTimeUnit duration);
86
87 Clock::time_point m_timeMark;
88 DefaultTimeUnit m_totalDuration;
89};
90
91inline Timer::Timer() :
92 m_timeMark(), m_totalDuration(DefaultTimeUnit::zero())
93{}
94
96{
97 m_timeMark = Clock::now();
98 m_totalDuration = DefaultTimeUnit::zero();
99
100 return *this;
101}
102
104{
105 m_timeMark = Clock::now();
106
107 return *this;
108}
109
111{
112 m_totalDuration += Clock::now() - m_timeMark;
113
114 return *this;
115}
116
118{
119 const auto currentTime = Clock::now();
120 const auto lapDuration = currentTime - m_timeMark;
121
122 m_timeMark = currentTime;
123 m_totalDuration += lapDuration;
124
125 return lapDuration;
126}
127
129{
130 const auto currentTime = Clock::now();
131 const auto lapDuration = currentTime - m_timeMark;
132
133 return lapDuration;
134}
135
137{
138 return m_totalDuration;
139}
140
141template<typename NumberType>
142inline NumberType Timer::markLapS()
143{
144 return castToNumber<NumberType, std::ratio<1>>(markLap());
145}
146
147template<typename NumberType>
148inline NumberType Timer::markLapMs()
149{
150 return castToNumber<NumberType, std::milli>(markLap());
151}
152
153template<typename NumberType>
154inline NumberType Timer::markLapUs()
155{
156 return castToNumber<NumberType, std::micro>(markLap());
157}
158
159template<typename NumberType>
160inline NumberType Timer::markLapNs()
161{
162 return castToNumber<NumberType, std::nano>(markLap());
163}
164
165template<typename NumberType>
166inline NumberType Timer::getDeltaS() const
167{
168 return castToNumber<NumberType, std::ratio<1>>(getDelta());
169}
170
171template<typename NumberType>
172inline NumberType Timer::getDeltaMs() const
173{
174 return castToNumber<NumberType, std::milli>(getDelta());
175}
176
177template<typename NumberType>
178inline NumberType Timer::getDeltaUs() const
179{
180 return castToNumber<NumberType, std::micro>(getDelta());
181}
182
183template<typename NumberType>
184inline NumberType Timer::getDeltaNs() const
185{
186 return castToNumber<NumberType, std::nano>(getDelta());
187}
188
189template<typename TimeRepresentation, typename TimePeriod>
190inline TimeRepresentation Timer::castToNumber(const DefaultTimeUnit duration)
191{
192 using DstDuration = std::chrono::duration<TimeRepresentation, TimePeriod>;
193
194 // We do not simply do `return DstDuration(duration);` since the implicit conversion will not
195 // allow any precision loss. Here we want behavior similar to `static_cast`.
196 return std::chrono::duration_cast<DstDuration>(duration).count();
197}
198
199}// end namespace ph
A timer. Measures relative time (not wall clock).
Definition Timer.h:14
NumberType markLapUs()
Definition Timer.h:154
NumberType getDeltaNs() const
Definition Timer.h:184
Timer()
Definition Timer.h:91
Clock::duration DefaultTimeUnit
Definition Timer.h:17
NumberType getDeltaMs() const
Definition Timer.h:172
Timer & accumulatedStart()
Effectively resumes the timer. Start the timer by continuing last result from stop().
Definition Timer.h:103
Timer & stop()
Stop the timer and record elapsed time.
Definition Timer.h:110
DefaultTimeUnit getDelta() const
Get the time elapsed between start() and stop(). Possibly accumulated. Result may be accumulated if a...
Definition Timer.h:136
NumberType markLapS()
Variants that return time in plain number with different units and types. As casting a numeric value ...
Definition Timer.h:142
NumberType markLapNs()
Definition Timer.h:160
NumberType markLapMs()
Definition Timer.h:148
DefaultTimeUnit peekLap() const
Get the time elapsed from last call to markLap(). Get the result of markLap() as if it was called,...
Definition Timer.h:128
NumberType getDeltaS() const
Definition Timer.h:166
Timer & start()
Zero and start the timer.
Definition Timer.h:95
DefaultTimeUnit markLap()
Get the time elapsed between calls to markLap(). If this is the first markLap() call,...
Definition Timer.h:117
std::chrono::steady_clock Clock
Definition Timer.h:16
NumberType getDeltaUs() const
Definition Timer.h:178
The root for all renderer implementations.
Definition EEngineProject.h:6