@@ -78,7 +78,7 @@ type InternalBenchmark struct {
78
78
}
79
79
80
80
// B is a type passed to [Benchmark] functions to manage benchmark
81
- // timing and to specify the number of iterations to run .
81
+ // timing and control the number of iterations.
82
82
//
83
83
// A benchmark ends when its Benchmark function returns or calls any of the methods
84
84
// FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods must be called
@@ -133,8 +133,7 @@ func (b *B) StartTimer() {
133
133
}
134
134
135
135
// StopTimer stops timing a test. This can be used to pause the timer
136
- // while performing complex initialization that you don't
137
- // want to measure.
136
+ // while performing steps that you don't want to measure.
138
137
func (b * B ) StopTimer () {
139
138
if b .timerOn {
140
139
b .duration += highPrecisionTimeSince (b .start )
@@ -387,7 +386,7 @@ func (b *B) loopSlowPath() bool {
387
386
b .ResetTimer ()
388
387
return true
389
388
}
390
- // Handles fixed time case
389
+ // Handles fixed iterations case
391
390
if b .benchTime .n > 0 {
392
391
if b .N < b .benchTime .n {
393
392
b .N = b .benchTime .n
@@ -396,31 +395,42 @@ func (b *B) loopSlowPath() bool {
396
395
}
397
396
return false
398
397
}
399
- // Handles fixed iteration count case
398
+ // Handles fixed time case
400
399
return b .stopOrScaleBLoop ()
401
400
}
402
401
403
- // Loop returns true until b.N calls has been made to it.
404
- //
405
- // A benchmark should either use Loop or contain an explicit loop from 0 to b.N, but not both.
406
- // After the benchmark finishes, b.N will contain the total number of calls to op, so the benchmark
407
- // may use b.N to compute other average metrics.
402
+ // Loop returns true as long as the benchmark should continue running.
408
403
//
409
- // The parameters and results of function calls inside the body of "for b.Loop() {...}" are guaranteed
410
- // not to be optimized away.
411
- // Also, the local loop scaling for b.Loop ensures the benchmark function containing the loop will only
412
- // be executed once, i.e. for such construct:
404
+ // A typical benchmark is structured like:
413
405
//
414
- // testing.Benchmark( func(b *testing.B) {
415
- // ...( setup)
416
- // for b.Loop() {
417
- // ...(benchmark logic)
418
- // }
419
- // ...(clean-up)
406
+ // func Benchmark (b *testing.B) {
407
+ // ... setup ...
408
+ // for b.Loop() {
409
+ // ... code to measure ...
410
+ // }
411
+ // ... cleanup ...
420
412
// }
421
413
//
422
- // The ...(setup) and ...(clean-up) logic will only be executed once.
423
- // Also benchtime=Nx (N>1) will result in exactly N executions instead of N+1 for b.N style loops.
414
+ // Loop resets the benchmark timer the first time it is called in a benchmark,
415
+ // so any setup performed prior to starting the benchmark loop does not count
416
+ // toward the benchmark measurement.
417
+ //
418
+ // The compiler never optimizes away calls to functions within the body of a
419
+ // "for b.Loop() { ... }" loop. This prevents surprises that can otherwise occur
420
+ // if the compiler determines that the result of a benchmarked function is
421
+ // unused. The loop must be written in exactly this form, and this only applies
422
+ // to calls syntactically between the curly braces of the loop. Optimizations
423
+ // are performed as usual in any functions called by the loop.
424
+ //
425
+ // After Loop returns false, b.N contains the total number of iterations that
426
+ // ran, so the benchmark may use b.N to compute other average metrics.
427
+ //
428
+ // Prior to the introduction of Loop, benchmarks were expected to contain an
429
+ // explicit loop from 0 to b.N. Benchmarks should either use Loop or contain a
430
+ // loop to b.N, but not both. Loop offers more automatic management of the
431
+ // benchmark timer, and runs each benchmark function only once per measurement,
432
+ // whereas b.N-based benchmarks must run the benchmark function (and any
433
+ // associated setup and cleanup) several times.
424
434
func (b * B ) Loop () bool {
425
435
if b .loopN != 0 && b .loopN < b .N {
426
436
b .loopN ++
0 commit comments