diff options
-rw-r--r-- | timer.h | 23 |
1 files changed, 19 insertions, 4 deletions
@@ -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<std::chrono::nanoseconds>(end - start_time); + end_time = std::chrono::system_clock::now(); + const auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(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<double> duration() { + return end_time - start_time; } }; |