Skip to content

Commit a5fdacc

Browse files
stanbot8LebedevRI
andauthored
Guard result.rms against division by zero (google#2317)
Co-authored-by: Roman Lebedev <lebedev.ri@gmail.com>
1 parent bffc191 commit a5fdacc

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎src/complexity.cc‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ LeastSq MinimalLeastSq(const std::vector<ComplexityN>& n,
115115

116116
// Normalized RMS by the mean of the observed values
117117
double mean = sigma_time / static_cast<double>(n.size());
118-
result.rms = std::sqrt(rms / static_cast<double>(n.size())) / mean;
118+
result.rms = std::fpclassify(mean) == FP_ZERO
119+
? 0.0
120+
: std::sqrt(rms / static_cast<double>(n.size())) / mean;
119121

120122
return result;
121123
}

‎test/complexity_test.cc‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,42 @@ ADD_COMPLEXITY_CASES(n_ms_test_name, big_o_n_ms_test_name, rms_o_n_ms_test_name,
298298
enum_auto_big_o_n, /*family_index=*/10, "ms",
299299
"1\\.[0-9]+e-03");
300300

301+
void BM_Complexity_O1_ZeroTime(benchmark::State& state) {
302+
for (auto _ : state) {
303+
state.SetIterationTime(0.0);
304+
}
305+
state.SetComplexityN(state.range(0));
306+
}
307+
BENCHMARK(BM_Complexity_O1_ZeroTime)
308+
->Range(1, 2)
309+
->UseManualTime()
310+
->Complexity(benchmark::o1);
311+
BENCHMARK(BM_Complexity_O1_ZeroTime)
312+
->Range(1, 2)
313+
->UseManualTime()
314+
->Complexity();
315+
BENCHMARK(BM_Complexity_O1_ZeroTime)
316+
->Range(1, 2)
317+
->UseManualTime()
318+
->Complexity([](benchmark::IterationCount) { return 1.0; });
319+
320+
constexpr char one_test_name_zero_time[] =
321+
"BM_Complexity_O1_ZeroTime/manual_time";
322+
constexpr char big_o_1_test_name_zero_time[] =
323+
"BM_Complexity_O1_ZeroTime/manual_time_BigO";
324+
constexpr char rms_o_1_test_name_zero_time[] =
325+
"BM_Complexity_O1_ZeroTime/manual_time_RMS";
326+
327+
ADD_COMPLEXITY_CASES(one_test_name_zero_time, big_o_1_test_name_zero_time,
328+
rms_o_1_test_name_zero_time, enum_auto_big_o_1, 11, "ns",
329+
"%float");
330+
ADD_COMPLEXITY_CASES(one_test_name_zero_time, big_o_1_test_name_zero_time,
331+
rms_o_1_test_name_zero_time, enum_auto_big_o_1, 12, "ns",
332+
"%float");
333+
ADD_COMPLEXITY_CASES(one_test_name_zero_time, big_o_1_test_name_zero_time,
334+
rms_o_1_test_name_zero_time, lambda_big_o_1, 13, "ns",
335+
"%float");
336+
301337
} // end namespace
302338

303339
// ========================================================================= //

0 commit comments

Comments
 (0)