#pragma once #include "pch.h" #include #include namespace pkNXHashDecoder { class Timer { public: /// Construct a new timer Timer() { Reset(); } /// Resets the timer to now void Reset() { start_ = Clock::now(); } auto Elapsed() const { return FMicro(Clock::now() - start_).count(); } auto ElapsedMillis() const { return FMili(Clock::now() - start_).count(); } private: using Clock = std::chrono::high_resolution_clock; using FMicro = std::chrono::duration; using FMili = std::chrono::duration; Clock::time_point start_; }; class LoopTimer : Timer { public: LoopTimer() = default; /// Resets the timer to now void Reset() { loops = 0; } auto ElapsedAverage() const { return Elapsed() / static_cast(loops); } auto ElapsedAverageMillis() const { return totalTime / static_cast(loops); } void Loop() { totalTime += ElapsedMillis(); ++loops; Timer::Reset(); } void Print() { std::cout << "[Timer]: " << loops << " loops, " << ElapsedAverageMillis() << "ms avg.\n"; } private: int loops = 0; float totalTime = 0.0f; }; class ScopedTimer { public: ScopedTimer(const std::string_view& name) : _name(name) { } ~ScopedTimer() { std::cout << "[Timer]: " << _name << " - " << _timer.ElapsedMillis() << "ms"; } private: std::string _name; Timer _timer; }; } // namespace pix::foundation