Skip to content

Commit 009749c

Browse files
committed
Use std::chrono::steady_clock to measure durations
steady_clock increases monotonically, whereas system_clock can go backwards. high_resolution_clock should just be avoided: https://en.cppreference.com/w/cpp/chrono/high_resolution_clock
1 parent 3b70761 commit 009749c

File tree

11 files changed

+33
-33
lines changed

11 files changed

+33
-33
lines changed

bench/coders/benchmark_coders.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void run_enwik8_impl(
8484
std::filesystem::remove(output);
8585
}
8686

87-
const auto start = std::chrono::system_clock::now();
87+
const auto start = std::chrono::steady_clock::now();
8888

8989
if constexpr (std::is_same_v<Encode, std::true_type>)
9090
{
@@ -95,7 +95,7 @@ void run_enwik8_impl(
9595
coder.decode(input, output);
9696
}
9797

98-
const auto end = std::chrono::system_clock::now();
98+
const auto end = std::chrono::steady_clock::now();
9999

100100
const auto duration = std::chrono::duration<double>(end - start);
101101
results.push_back(duration.count());

bench/json/benchmark_json.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ CATCH_TEST_CASE("JSON", "[bench]")
104104

105105
for (std::size_t i = 0; i < s_iterations; ++i)
106106
{
107-
const auto start = std::chrono::system_clock::now();
107+
const auto start = std::chrono::steady_clock::now();
108108
parser.second->parse(file);
109-
const auto end = std::chrono::system_clock::now();
109+
const auto end = std::chrono::steady_clock::now();
110110

111111
const auto duration = std::chrono::duration<double>(end - start);
112112
results.push_back(duration.count());

bench/string/benchmark_string.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void run_format_test(std::string &&name)
111111

112112
for (std::size_t i = 0; i < s_iterations; ++i)
113113
{
114-
const auto start = std::chrono::system_clock::now();
114+
const auto start = std::chrono::steady_clock::now();
115115

116116
if constexpr (std::is_same_v<WithFloats, std::true_type>)
117117
{
@@ -122,7 +122,7 @@ void run_format_test(std::string &&name)
122122
formatter.second->format_without_floats();
123123
}
124124

125-
const auto end = std::chrono::system_clock::now();
125+
const auto end = std::chrono::steady_clock::now();
126126

127127
const auto duration = std::chrono::duration<double>(end - start);
128128
results.push_back(duration.count());

fly/coders/coder.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ namespace {
1919

2020
template <typename SizeType>
2121
void log_encoder_stats(
22-
std::chrono::time_point<std::chrono::system_clock> start,
22+
std::chrono::steady_clock::time_point start,
2323
SizeType decoded_size,
2424
SizeType encoded_size)
2525
{
26-
const auto end = std::chrono::system_clock::now();
26+
const auto end = std::chrono::steady_clock::now();
2727
const auto ratio = (static_cast<double>(decoded_size) - encoded_size) / decoded_size;
2828

2929
LOGD(
@@ -36,11 +36,11 @@ namespace {
3636

3737
template <typename SizeType>
3838
void log_decoder_stats(
39-
std::chrono::time_point<std::chrono::system_clock> start,
39+
std::chrono::steady_clock::time_point start,
4040
SizeType encoded_size,
4141
SizeType decoded_size)
4242
{
43-
const auto end = std::chrono::system_clock::now();
43+
const auto end = std::chrono::steady_clock::now();
4444

4545
LOGD(
4646
"Decoded {} bytes to {} bytes in {:.2f} seconds",
@@ -54,7 +54,7 @@ namespace {
5454
//==================================================================================================
5555
bool Encoder::encode_string(const std::string &decoded, std::string &encoded)
5656
{
57-
const auto start = std::chrono::system_clock::now();
57+
const auto start = std::chrono::steady_clock::now();
5858
bool successful = false;
5959

6060
std::istringstream input(decoded, s_input_mode);
@@ -79,7 +79,7 @@ bool Encoder::encode_file(
7979
const std::filesystem::path &decoded,
8080
const std::filesystem::path &encoded)
8181
{
82-
const auto start = std::chrono::system_clock::now();
82+
const auto start = std::chrono::steady_clock::now();
8383
bool successful = false;
8484
{
8585
std::ifstream input(decoded, s_input_mode);
@@ -112,7 +112,7 @@ bool BinaryEncoder::encode_internal(std::istream &decoded, std::ostream &encoded
112112
//==================================================================================================
113113
bool Decoder::decode_string(const std::string &encoded, std::string &decoded)
114114
{
115-
const auto start = std::chrono::system_clock::now();
115+
const auto start = std::chrono::steady_clock::now();
116116
bool successful = false;
117117

118118
std::istringstream input(encoded, s_input_mode);
@@ -137,7 +137,7 @@ bool Decoder::decode_file(
137137
const std::filesystem::path &encoded,
138138
const std::filesystem::path &decoded)
139139
{
140-
const auto start = std::chrono::system_clock::now();
140+
const auto start = std::chrono::steady_clock::now();
141141
bool successful = false;
142142
{
143143
std::ifstream input(encoded, s_input_mode);

fly/logger/logger.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Logger::Logger(
2121
m_config(config),
2222
m_sink(std::move(sink)),
2323
m_task_runner(task_runner),
24-
m_start_time(std::chrono::high_resolution_clock::now())
24+
m_start_time(std::chrono::steady_clock::now())
2525
{
2626
}
2727

@@ -142,7 +142,7 @@ void Logger::log(Log::Level level, Log::Trace &&trace, std::string &&message)
142142
return;
143143
}
144144

145-
const auto now = std::chrono::high_resolution_clock::now();
145+
const auto now = std::chrono::steady_clock::now();
146146

147147
if (m_task_runner)
148148
{
@@ -169,7 +169,7 @@ void Logger::log_to_sink(
169169
Log::Level level,
170170
Log::Trace &&trace,
171171
std::string &&message,
172-
std::chrono::high_resolution_clock::time_point time)
172+
std::chrono::steady_clock::time_point time)
173173
{
174174
const std::chrono::duration<double, std::milli> elapsed = time - m_start_time;
175175

fly/logger/logger.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class Logger : public std::enable_shared_from_this<Logger>
477477
Log::Level level,
478478
Log::Trace &&trace,
479479
std::string &&message,
480-
std::chrono::high_resolution_clock::time_point time);
480+
std::chrono::steady_clock::time_point time);
481481

482482
const std::string m_name;
483483

@@ -487,7 +487,7 @@ class Logger : public std::enable_shared_from_this<Logger>
487487
std::shared_ptr<SequencedTaskRunner> m_task_runner;
488488
std::atomic_bool m_last_task_failed {true};
489489

490-
const std::chrono::high_resolution_clock::time_point m_start_time;
490+
const std::chrono::steady_clock::time_point m_start_time;
491491
std::uintmax_t m_index {0};
492492
};
493493

fly/path/mac/path_monitor_impl.mm

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ ino_t get_inode_id(const std::filesystem::path &path)
8585

8686
while (time_remaining != std::chrono::milliseconds::zero())
8787
{
88-
const auto start = std::chrono::high_resolution_clock::now();
88+
const auto start = std::chrono::steady_clock::now();
8989

9090
if (m_event_queue.pop(event, time_remaining))
9191
{
9292
handle_event(std::move(event));
9393
}
9494

95-
const auto end = std::chrono::high_resolution_clock::now();
95+
const auto end = std::chrono::steady_clock::now();
9696
const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
9797

9898
if (duration >= time_remaining)

fly/system/mac/system_monitor_impl.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SystemMonitorImpl : public SystemMonitor
4343

4444
std::uint64_t m_prev_process_user_time {0};
4545
std::uint64_t m_prev_process_system_time {0};
46-
std::chrono::high_resolution_clock::time_point m_prev_time;
46+
std::chrono::steady_clock::time_point m_prev_time;
4747
};
4848

4949
} // namespace fly

fly/system/mac/system_monitor_impl.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
return;
7373
}
7474

75-
const auto now = std::chrono::high_resolution_clock::now();
75+
const auto now = std::chrono::steady_clock::now();
7676
const auto user = time_value_to_microseconds(thread_times.user_time);
7777
const auto system = time_value_to_microseconds(thread_times.system_time);
7878

test/main.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ class FlyReporter : public Catch::ConsoleReporter
3131
void testRunStarting(const Catch::TestRunInfo &info) override
3232
{
3333
Catch::ConsoleReporter::testRunStarting(info);
34-
m_test_start = std::chrono::system_clock::now();
34+
m_test_start = std::chrono::steady_clock::now();
3535
}
3636

3737
void testRunEnded(const Catch::TestRunStats &stats) override
3838
{
3939
Catch::ConsoleReporter::testRunEnded(stats);
4040

41-
const auto end = std::chrono::system_clock::now();
41+
const auto end = std::chrono::steady_clock::now();
4242
const auto duration = std::chrono::duration<double>(end - m_test_start);
4343

4444
// ConsoleReporter prints a second newline above, so go up one line before logging the time.
@@ -53,7 +53,7 @@ class FlyReporter : public Catch::ConsoleReporter
5353

5454
Catch::ConsoleReporter::testCaseStarting(info);
5555

56-
m_current_test_case_start = std::chrono::system_clock::now();
56+
m_current_test_case_start = std::chrono::steady_clock::now();
5757
m_current_test_case = info.name;
5858
}
5959

@@ -70,7 +70,7 @@ class FlyReporter : public Catch::ConsoleReporter
7070

7171
void testCaseEnded(const Catch::TestCaseStats &stats) override
7272
{
73-
const auto end = std::chrono::system_clock::now();
73+
const auto end = std::chrono::steady_clock::now();
7474
const auto duration = std::chrono::duration<double>(end - m_current_test_case_start);
7575

7676
const std::string &name = stats.testInfo->name;
@@ -101,9 +101,9 @@ class FlyReporter : public Catch::ConsoleReporter
101101
}
102102

103103
private:
104-
std::chrono::system_clock::time_point m_test_start;
104+
std::chrono::steady_clock::time_point m_test_start;
105105

106-
std::chrono::system_clock::time_point m_current_test_case_start;
106+
std::chrono::steady_clock::time_point m_current_test_case_start;
107107
std::string m_current_test_case;
108108
};
109109

test/task/task.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class MarkerTask
9696
class TimerTask : public fly::Task
9797
{
9898
public:
99-
TimerTask() noexcept : m_start_time(std::chrono::high_resolution_clock::now())
99+
TimerTask() noexcept : m_start_time(std::chrono::steady_clock::now())
100100
{
101101
}
102102

@@ -107,12 +107,12 @@ class TimerTask : public fly::Task
107107

108108
void run()
109109
{
110-
m_stop_time = std::chrono::high_resolution_clock::now();
110+
m_stop_time = std::chrono::steady_clock::now();
111111
}
112112

113113
private:
114-
std::chrono::high_resolution_clock::time_point m_start_time;
115-
std::chrono::high_resolution_clock::time_point m_stop_time;
114+
std::chrono::steady_clock::time_point m_start_time;
115+
std::chrono::steady_clock::time_point m_stop_time;
116116
};
117117

118118
} // namespace

0 commit comments

Comments
 (0)