| 18 | #include <GameToolbox/log.h> |
| 19 | |
| 20 | class BenchmarkTimer |
| 21 | { |
| 22 | public: |
| 23 | BenchmarkTimer(const char* name) |
| 24 | : m_Name(name), m_Stopped(false) |
| 25 | { |
| 26 | m_StartTimepoint = std::chrono::high_resolution_clock::now(); |
| 27 | } |
| 28 | |
| 29 | ~BenchmarkTimer() |
| 30 | { |
| 31 | if (!m_Stopped) |
| 32 | Stop(); |
| 33 | } |
| 34 | |
| 35 | void Stop() |
| 36 | { |
| 37 | auto endTimepoint = std::chrono::high_resolution_clock::now(); |
| 38 | |
| 39 | long long start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count(); |
| 40 | long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count(); |
| 41 | |
| 42 | auto duration = start - end; |
| 43 | double ms = (duration * 0.001) * -1; |
| 44 | |
| 45 | GameToolbox::log("TIMER [{}]|[{}]", m_Name, ms); |
| 46 | |
| 47 | m_Stopped = true; |
| 48 | } |
| 49 | private: |
| 50 | const char* m_Name; |
| 51 | std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimepoint; |
| 52 | bool m_Stopped; |
| 53 | }; |