-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
remove the unnecessary call to ResetTimer and StopTimer #6185
remove the unnecessary call to ResetTimer and StopTimer #6185
Conversation
remove the unnecessary call to ResetTimer and StopTimer
Is it not more accurate (i.e. the measurements of the timer) to call this in the function body rather than the caller? |
The answer is no need. First of all, i will give you a test data:
All the test cases tailed with X is the version removed ResetTimer/StopTimer. and you can see, there is no observable difference between them. and in the theory analysis, testing will do increase b.N to make the convergence of the op time and remove the effect of the non inline call to bench function. finally, it not a better practice to add ResetTImer/StopTImer wrapper the whole benchmark function. an the testing package already to a better work for us. |
@zasweq please help solve this issue when you have time. |
I'm seeing the same or greater nanoseconds/operation for each benchmark. Thoughts @easwars? |
half of thie tests show the version removed is more faster, maybe you misunderstand the report? @zasweq |
Yes, I see 2/5 ones getting faster, 1 of them equal, and 2/5ths getting slower. Thus, this doesn't seem to improve it. |
This code was written 6 years ago, and at that time we probably didn't understand how to use the benchmarking support in the |
@ethanvc : Could you please rebase your branch to master. We recently fixed a flaky test which is affecting you branch. Thanks. |
@zasweq : I'm fine with these changes. Assigning back for second set of eyes. |
Ok I'll approve it then. |
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.
LGTM.
done |
This MR is not intended to improve performance or make these benchmark tests more accurate. Instead, the purpose of this MR is to use the testing library correctly. From an intent perspective, wrapping the benchmark test function with ResetTimer and StopTimer is meant to eliminate the error caused by the inability to inline (function pointer calls) when the testing library invokes the base test function. However, the original author did not consider that the testing library would adjust the value of N to mitigate this overhead, so we do not need to worry about this impact when using the testing library. As you can see, adding ResetTimer does not affect the benchmark test results; it is doing unnecessary work. To my understanding, this has the same problem as the following code: Copy code
func add(n int) int{
n += 0
n += 1
return n
} In the function above, the n += 0 statement is meaningless and only confuses maintainers. As an influential open-source library, I believe that both procedural correctness and result correctness are equally important. Although these two versions of the code have no impact on the benchmark test results, the code itself may mislead others. It may lead people to believe that calling ResetTimer and StopTimer before and after a benchmark test can perceptibly improve the accuracy of the benchmark test. However, this understanding is incorrect. |
Ok, sounds good, thanks for the contribution/explanation :). |
remove the unnecessary call to ResetTimer and StopTimer. which will misleading other people.
In the implemation of testing.B:
The testing framework already called ResetTimer/StopTimer before call user's benchmark function.
RELEASE NOTES: none