-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExecutionTimer.cpp
35 lines (29 loc) · 905 Bytes
/
ExecutionTimer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "ExecutionTimer.hpp"
execution_timer::execution_timer() : beginning_(clock::now())
{
}
double execution_timer::elapsed_seconds() const
{
return std::chrono::duration_cast<second>(clock::now() - beginning_).count();
}
void execution_timer::reset()
{
beginning_ = clock::now();
}
bool execution_timer::is_expired(const int milliseconds) const
{
const auto elapsed_seconds_v = elapsed_seconds();
return elapsed_seconds_v * 1000 > milliseconds;
}
inline auto round_up(const double value, const int decimal_places)
{
const auto multiplier = std::pow(10.0, decimal_places);
return std::ceil(value * multiplier) / multiplier;
}
std::string execution_timer::build_log_message(const std::string& action) const
{
const auto elapsed = elapsed_seconds();
std::stringstream message_builder;
message_builder << action << " took " << round_up(elapsed, 2) << " s";
return message_builder.str();
}