Skip to content

Commit 865cf5c

Browse files
Jorropogopherbot
authored andcommitted
testing: improve the documentation around b.N
Fixes #67137 - Make it clear the benchmark function is called multiple times. - Demonstrate range over int. Change-Id: I7e993d938b0351012cdd4aed8528951e0ad406ae Reviewed-on: https://go-review.googlesource.com/c/go/+/582835 Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Damien Neil <dneil@google.com>
1 parent 660a071 commit 865cf5c

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/testing/testing.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@
7272
// A sample benchmark function looks like this:
7373
//
7474
// func BenchmarkRandInt(b *testing.B) {
75-
// for i := 0; i < b.N; i++ {
75+
// for range b.N {
7676
// rand.Int()
7777
// }
7878
// }
7979
//
8080
// The benchmark function must run the target code b.N times.
81-
// During benchmark execution, b.N is adjusted until the benchmark function lasts
82-
// long enough to be timed reliably. The output
81+
// It is called multiple times with b.N adjusted until the
82+
// benchmark function lasts long enough to be timed reliably.
83+
// The output
8384
//
8485
// BenchmarkRandInt-8 68453040 17.8 ns/op
8586
//
@@ -91,7 +92,7 @@
9192
// func BenchmarkBigLen(b *testing.B) {
9293
// big := NewBig()
9394
// b.ResetTimer()
94-
// for i := 0; i < b.N; i++ {
95+
// for range b.N {
9596
// big.Len()
9697
// }
9798
// }

0 commit comments

Comments
 (0)