From 74af79bcf6b1efde7b91ac3e519338c6815efda9 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Mon, 22 May 2023 22:40:47 +0300 Subject: [PATCH] fmt,math/big,net/url: fixes to old Benchmarks b.ResetTimer used to also stop the timer, however it does not anymore. These benchmarks hadn't been fixed and as a result ended up measuring some additional things. Also, make some for loops more conventional. Change-Id: I76ca68456d85eec51722a80587e5b2c9f5d836a1 Reviewed-on: https://go-review.googlesource.com/c/go/+/496996 Run-TryBot: Damien Neil Auto-Submit: Ian Lance Taylor Auto-Submit: Damien Neil TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Reviewed-by: Damien Neil Run-TryBot: Ian Lance Taylor --- src/fmt/scan_test.go | 12 ++++++------ src/math/big/int_test.go | 6 +----- src/net/url/url_test.go | 1 - 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/fmt/scan_test.go b/src/fmt/scan_test.go index e8c5769924..a4f80c23c2 100644 --- a/src/fmt/scan_test.go +++ b/src/fmt/scan_test.go @@ -1096,10 +1096,10 @@ func testScanInts(t *testing.T, scan func(*RecursiveInt, *bytes.Buffer) error) { } func BenchmarkScanInts(b *testing.B) { - b.ResetTimer() + b.StopTimer() ints := makeInts(intCount) var r RecursiveInt - for i := b.N - 1; i >= 0; i-- { + for i := 0; i < b.N; i++ { buf := bytes.NewBuffer(ints) b.StartTimer() scanInts(&r, buf) @@ -1108,10 +1108,10 @@ func BenchmarkScanInts(b *testing.B) { } func BenchmarkScanRecursiveInt(b *testing.B) { - b.ResetTimer() + b.StopTimer() ints := makeInts(intCount) var r RecursiveInt - for i := b.N - 1; i >= 0; i-- { + for i := 0; i < b.N; i++ { buf := bytes.NewBuffer(ints) b.StartTimer() Fscan(buf, &r) @@ -1120,10 +1120,10 @@ func BenchmarkScanRecursiveInt(b *testing.B) { } func BenchmarkScanRecursiveIntReaderWrapper(b *testing.B) { - b.ResetTimer() + b.StopTimer() ints := makeInts(intCount) var r RecursiveInt - for i := b.N - 1; i >= 0; i-- { + for i := 0; i < b.N; i++ { buf := struct{ io.Reader }{strings.NewReader(string(ints))} b.StartTimer() Fscan(buf, &r) diff --git a/src/math/big/int_test.go b/src/math/big/int_test.go index 2800d8f247..dfbc17242d 100644 --- a/src/math/big/int_test.go +++ b/src/math/big/int_test.go @@ -254,7 +254,7 @@ func TestBinomial(t *testing.T) { func BenchmarkBinomial(b *testing.B) { var z Int - for i := b.N - 1; i >= 0; i-- { + for i := 0; i < b.N; i++ { z.Binomial(1000, 990) } } @@ -1425,7 +1425,6 @@ func BenchmarkBitset(b *testing.B) { z := new(Int) z.SetBit(z, 512, 1) b.ResetTimer() - b.StartTimer() for i := b.N - 1; i >= 0; i-- { z.SetBit(z, i&512, 1) } @@ -1435,7 +1434,6 @@ func BenchmarkBitsetNeg(b *testing.B) { z := NewInt(-1) z.SetBit(z, 512, 0) b.ResetTimer() - b.StartTimer() for i := b.N - 1; i >= 0; i-- { z.SetBit(z, i&512, 0) } @@ -1445,7 +1443,6 @@ func BenchmarkBitsetOrig(b *testing.B) { z := new(Int) altSetBit(z, z, 512, 1) b.ResetTimer() - b.StartTimer() for i := b.N - 1; i >= 0; i-- { altSetBit(z, z, i&512, 1) } @@ -1455,7 +1452,6 @@ func BenchmarkBitsetNegOrig(b *testing.B) { z := NewInt(-1) altSetBit(z, z, 512, 0) b.ResetTimer() - b.StartTimer() for i := b.N - 1; i >= 0; i-- { altSetBit(z, z, i&512, 0) } diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go index 577cf631c8..23c5c581c5 100644 --- a/src/net/url/url_test.go +++ b/src/net/url/url_test.go @@ -1116,7 +1116,6 @@ func TestResolvePath(t *testing.T) { } func BenchmarkResolvePath(b *testing.B) { - b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { resolvePath("a/b/c", ".././d") -- 2.50.0