12
12
#include < util/types.h>
13
13
14
14
#include < chrono>
15
+ #include < optional>
15
16
#include < string>
16
17
17
18
@@ -28,14 +29,14 @@ class Timer
28
29
std::string prefix,
29
30
std::string end_msg,
30
31
BCLog::LogFlags log_category = BCLog::LogFlags::ALL,
31
- bool msg_on_completion = true ) :
32
- m_prefix (std::move(prefix)),
33
- m_title (std::move(end_msg)),
34
- m_log_category (log_category),
35
- m_message_on_completion (msg_on_completion)
32
+ bool msg_on_completion = true )
33
+ : m_prefix(std::move(prefix)),
34
+ m_title (std::move(end_msg)),
35
+ m_log_category(log_category),
36
+ m_message_on_completion(msg_on_completion)
36
37
{
37
38
this ->Log (strprintf (" %s started" , m_title));
38
- m_start_t = GetTime< std::chrono::microseconds> ();
39
+ m_start_t = std::chrono::steady_clock::now ();
39
40
}
40
41
41
42
~Timer ()
@@ -60,24 +61,25 @@ class Timer
60
61
61
62
std::string LogMsg (const std::string& msg)
62
63
{
63
- const auto end_time = GetTime< std::chrono::microseconds>() - m_start_t ;
64
- if (m_start_t . count () <= 0 ) {
64
+ const auto end_time{ std::chrono::steady_clock::now ()} ;
65
+ if (! m_start_t ) {
65
66
return strprintf (" %s: %s" , m_prefix, msg);
66
67
}
68
+ const auto duration{end_time - *m_start_t };
67
69
68
70
if constexpr (std::is_same<TimeType, std::chrono::microseconds>::value) {
69
- return strprintf (" %s: %s (%iμs)" , m_prefix, msg, end_time. count ( ));
71
+ return strprintf (" %s: %s (%iμs)" , m_prefix, msg, Ticks<std::chrono::microseconds>(duration ));
70
72
} else if constexpr (std::is_same<TimeType, std::chrono::milliseconds>::value) {
71
- return strprintf (" %s: %s (%.2fms)" , m_prefix, msg, end_time. count () * 0.001 );
73
+ return strprintf (" %s: %s (%.2fms)" , m_prefix, msg, Ticks<MillisecondsDouble>(duration) );
72
74
} else if constexpr (std::is_same<TimeType, std::chrono::seconds>::value) {
73
- return strprintf (" %s: %s (%.2fs)" , m_prefix, msg, end_time. count () * 0.000001 );
75
+ return strprintf (" %s: %s (%.2fs)" , m_prefix, msg, Ticks<SecondsDouble>(duration) );
74
76
} else {
75
77
static_assert (ALWAYS_FALSE<TimeType>, " Error: unexpected time type" );
76
78
}
77
79
}
78
80
79
81
private:
80
- std::chrono::microseconds m_start_t {};
82
+ std::optional<std:: chrono::steady_clock::time_point> m_start_t {};
81
83
82
84
// ! Log prefix; usually the name of the function this was created in.
83
85
const std::string m_prefix;
0 commit comments