#pragma once #include #include #include #define time_now() std::chrono::high_resolution_clock::now() using ns = std::chrono::nanoseconds; using ms = std::chrono::milliseconds; template auto to_duration(const std::chrono::duration &delta) { return std::chrono::duration_cast(delta).count(); } template auto timer(F func, Args &&... args) { auto start_time = time_now(); func(std::forward(args)...); return to_duration(time_now() - start_time); } // idea from Optimized C++ Kurt Guntheroth 2016 // TODO: make more modular and easier to use class Stopwatch { public: Stopwatch() : start(std::chrono::system_clock::time_point::min()) {} void Clear() { start = std::chrono::system_clock::time_point::min(); } bool IsStarted() const { return (start != std::chrono::system_clock::time_point::min()); } void Start() { start = std::chrono::system_clock::now(); } unsigned long GetMs() { if (IsStarted()) { std::chrono::system_clock::duration diff; diff = std::chrono::system_clock::now() - start; return (unsigned) (std::chrono::duration_cast(diff).count()); } return 0; } ~Stopwatch() { std::cout << "Time: " << GetMs() << "ms" << std::endl; } private: std::chrono::system_clock::time_point start; };