diff options
author | Keuin <[email protected]> | 2022-04-17 11:57:55 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-04-17 11:57:55 +0800 |
commit | 3c57ce96a98ea00da80c4a2cef33e9db21c618ac (patch) | |
tree | 778db92047115b3ed7760d8da9484aee9588f1e4 /timer.h | |
parent | 3e7ca8a48e9a788882fa201b14da2eb5b6cf8bf0 (diff) |
Make timer support silent output and get duration.
Diffstat (limited to 'timer.h')
-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; } }; |