VOX
A little voxel engine
Loading...
Searching...
No Matches
Timer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <array>
5
6template <size_t ChronoCount = 1, size_t HistorySize = 1000>
7class Timer
8{
9
10public:
11
13 {
14 start();
15 }
16
17 ~Timer() = default;
18
19 void start(const int index = 0)
20 {
21 m_data[index].start_time = std::chrono::steady_clock::now().time_since_epoch();
22 }
23
24 void stop(const int index = 0)
25 {
26 m_data[index].end_time = std::chrono::steady_clock::now().time_since_epoch();
27 std::chrono::nanoseconds delta_time = m_data[index].end_time - m_data[index].start_time;
28
29 m_data[index].total_time += delta_time - m_data[index].history[0];
30 std::shift_left(m_data[index].history.begin(), m_data[index].history.end(), 1);
31 m_data[index].history[HistorySize - 1] = delta_time;
32 }
33
34 template <typename T = std::chrono::nanoseconds>
35 double elapsed(const int index = 0) const
36 {
37 return std::chrono::duration_cast<T>(m_data[index].delta_time).count();
38 }
39
40 template <typename T = std::chrono::nanoseconds>
41 double average(const int index = 0) const
42 {
43 return std::chrono::duration_cast<T>(m_data[index].total_time).count() / HistorySize;
44 }
45
46private:
47
48 struct Data
49 {
50 std::chrono::nanoseconds start_time;
51 std::chrono::nanoseconds end_time;
52 std::chrono::nanoseconds delta_time;
53
54 std::chrono::nanoseconds total_time;
55
56 std::array<std::chrono::nanoseconds, HistorySize> history;
57 };
58
59 std::array<Data, ChronoCount> m_data;
60
61};
Definition: Timer.hpp:8
~Timer()=default
void start(const int index=0)
Definition: Timer.hpp:19
Timer()
Definition: Timer.hpp:12
void stop(const int index=0)
Definition: Timer.hpp:24
double elapsed(const int index=0) const
Definition: Timer.hpp:35
double average(const int index=0) const
Definition: Timer.hpp:41