From 4b6d9a0ce857e6996d60370ce82ce6855651a24f Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Thu, 26 Nov 2020 09:23:52 +0200 Subject: [PATCH] utils: add RAII stopwatch For benchmarking --- lib/utils/mu-utils.hh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index fe8ff301..b83e352f 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -131,6 +131,25 @@ constexpr int64_t to_s (Duration d) { return to_unit(d); constexpr int64_t to_ms (Duration d) { return to_unit(d); } constexpr int64_t to_us (Duration d) { return to_unit(d); } +struct StopWatch { + using Clock = std::chrono::steady_clock; + StopWatch (const std::string name): + start_{Clock::now()}, + name_{name} {} + ~StopWatch() { + const auto us{to_us(Clock::now()-start_)}; + if (us > 2000000) + g_debug ("%s: finished after %0.1f s", name_.c_str(), us/1000000.0); + else if (us > 2000) + g_debug ("%s: finished after %0.1f ms", name_.c_str(), us/1000.0); + else + g_debug ("%s: finished after %" G_GINT64_FORMAT " us", name_.c_str(), us); + } +private: + Clock::time_point start_; + std::string name_; +}; + /** * See g_canonicalize_filename *