As Michael Jones said, you still need to play by the testing package's
benchmark rules for it to be able to benchmark your code.
So something along these lines.
func BenchmarkMarshalSample(b *testing.B) {
for i:=0; i < b.N; i++ {
var sum int64
// start := time.Now()
* Warren Bare [200519 13:53]:
> OK, I added a sum of the rand to the demo code and the results are the
> same. Since it is displaying the sum, it seems clear that the code is not
> optimized away.
>
> Again, I am NOT trying to time each iteration of the loop. This is a
> minimal
OK, I got it. You were trying to change the code I was benchmarking, and I
did not realize you were trying to say a benchmark must have the ability to
run b.N times.
Yes, that was the problem. This fixed it:
func BenchmarkMarshalSample(b *testing.B) {
var sum int64
for i:= 0; i <
OK, I added a sum of the rand to the demo code and the results are the
same. Since it is displaying the sum, it seems clear that the code is not
optimized away.
Again, I am NOT trying to time each iteration of the loop. This is a
minimal demonstration of a weirdness I was seeing in my own
as was explained, the loop needs to be "for i:=0; i < b.N; i++"
as was mentioned, the compiler's dead code elimination efforts can
frustrate benchmarking. they way to make sure the test code survives is to
not let it be dead code. for example
// external var dummy
func
for i:=0; i < b.N; i++ {
>You are supposed to run the loop b.N times, not some fixed constant.
I understand. This is a simulation of a single bigger task that takes a
while. I'm not trying to time the rand function inside the loop. The loop
is simply to burn time. This simple function is a minimal example that
You are supposed to run the loop b.N times, not
some fixed constant. Also make sure the compiler
doesn't optimize away the whole function.
V.
On Tuesday, 19 May 2020 18:20:43 UTC+2, Warren Bare wrote:
>
> Hi Folks,
>
> I'm getting weird results from Benchmark. Maybe someone can help me
>