From d94d8ccfd3b5249a78d1793b7208d01488b3b1e2 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Fri, 16 Dec 2016 04:01:23 +0100 Subject: [PATCH] feat(debug_util): Scoped execution timer --- include/debug.hpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/include/debug.hpp b/include/debug.hpp index 35b6b43f..59892f56 100644 --- a/include/debug.hpp +++ b/include/debug.hpp @@ -12,11 +12,26 @@ POLYBAR_NS namespace debug_util { - template - void loop(const T& expr, size_t iterations) noexcept { - while (iterations--) { - expr(); + /** + * Wrapper that starts tracking the time when created + * and reports the duration when it goes out of scope + */ + class scope_timer { + public: + using clock_t = std::chrono::high_resolution_clock; + using duration_t = std::chrono::milliseconds; + + explicit scope_timer() : m_start(clock_t::now()) {} + ~scope_timer() { + std::cout << std::chrono::duration_cast(clock_t::now() - m_start).count() << "ms" << std::endl; } + + private: + clock_t::time_point m_start; + }; + + inline unique_ptr make_scope_timer() { + return make_unique(); } template @@ -28,11 +43,6 @@ namespace debug_util { << "ms" << std::endl; } - template - void execution_speed(const T& expr, size_t iterations) noexcept { - execution_speed([=] { loop(expr, iterations); }); - } - template void memory_usage(const T& object) noexcept { std::cout << "memory usage: " << sizeof(object) << "b" << std::endl;