-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ClockEstimate: Correct Average on Abort #1490
Conversation
The clock estimator has a potential division by zero. Using `iteration + 1` seems also more logical to me for an average. Found with coverity in a downstream project.
Codecov Report
@@ Coverage Diff @@
## master #1490 +/- ##
==========================================
- Coverage 80.42% 80.39% -0.03%
==========================================
Files 121 121
Lines 3421 3426 +5
==========================================
+ Hits 2751 2754 +3
- Misses 670 672 +2 |
1 similar comment
Codecov Report
@@ Coverage Diff @@
## master #1490 +/- ##
==========================================
- Coverage 80.42% 80.39% -0.03%
==========================================
Files 121 121
Lines 3421 3426 +5
==========================================
+ Hits 2751 2754 +3
- Misses 670 672 +2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think that you should increase the i
only in case of i == 0
.
No, the sum is already calculated at that point, should definitely be |
so you want to divide sum by 2 in case of i=1? |
the sum starts with zero and adds for in the second iteration, The last iteration is |
okay in that case i acknowledge my mistake :) |
Is it possible to rename this PR? Division by Zero is a bit misleading because imo this will never happen. The actual problem (as correctly pointed out by the author) is that the mean/average is actually inaccurate. Edit: Reading through the code again, I think there is actually no problem here. The mean calculation is already correct. Note that the iteration is using pre-increment. Catch2/include/internal/catch_timer.cpp Line 28 in 20dd08e
Edit2:I am just stupid, ignore edit1. |
Hi @hbina, this is zero-indexed for-loop, with range for // main.cpp
#include <cstddef>
#include <iostream>
int main()
{
std::size_t iterations = 4u;
for( std::size_t i = 0; i < iterations; ++i ) {
std::cout << i << std::endl;
}
return 0;
} Compile & run: $ g++ main.cpp && ./a.out
0
1
2
3
$ Here is how for-loops work in C++:
|
@horenmar just in case: this is a simple fix for a corner-case, if you might want to take a look :) |
Yeah, the estimate is wrong in case of early-abort, and veeeeeeery potentially can cause a division by zero (if your clock resolution is worse than 3 seconds, 🤷♂️ ). There is no reason not to fix it though. |
Description
The clock estimator has a potential division by zero and the average is wrong on abort. Using
iteration + 1
would be correct for an average and is consistent with the non-aborted result which is alsosum / (max(i)+1)
.Seen via coverity in a downstream project.