From 3c57ce96a98ea00da80c4a2cef33e9db21c618ac Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 17 Apr 2022 11:57:55 +0800 Subject: Make timer support silent output and get duration. --- timer.h | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'timer.h') diff --git a/timer.h b/timer.h index 1f100cd..68774c5 100644 --- a/timer.h +++ b/timer.h @@ -11,16 +11,31 @@ class timer { private: typeof(std::chrono::system_clock::now()) start_time; + typeof(std::chrono::system_clock::now()) end_time; + bool silent; public: + timer() : silent{false} {} + + explicit timer(bool silent) : silent{silent} {} + void start_measure() { - fprintf(stderr, "Start timing...\n"); + if (!silent) { + fprintf(stderr, "Start timing...\n"); + } start_time = std::chrono::system_clock::now(); } void stop_measure() { - const auto end = std::chrono::system_clock::now(); - const auto duration = std::chrono::duration_cast(end - start_time); + end_time = std::chrono::system_clock::now(); + const auto duration = std::chrono::duration_cast(end_time - start_time); const auto secs = 1e-3 * duration.count() * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den; - fprintf(stderr, "Stop timing. Duration: %fs\n", secs); + if (!silent) { + fprintf(stderr, "Stop timing. Duration: %fs\n", secs); + } + } + + // Get seconds elapsed. + std::chrono::duration duration() { + return end_time - start_time; } }; -- cgit v1.2.3