-
Notifications
You must be signed in to change notification settings - Fork 18k
/
Copy pathexample_loop_test.go
48 lines (41 loc) · 1.21 KB
/
example_loop_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testing_test
import (
"math/rand/v2"
"testing"
)
// ExBenchmark shows how to use b.Loop in a benchmark.
//
// (If this were a real benchmark, not an example, this would be named
// BenchmarkSomething.)
func ExBenchmark(b *testing.B) {
// Generate a large random slice to use as an input.
// Since this is done before the first call to b.Loop(),
// it doesn't count toward the benchmark time.
input := make([]int, 128<<10)
for i := range input {
input[i] = rand.Int()
}
// Perform the benchmark.
for b.Loop() {
// Normally, the compiler would be allowed to optimize away the call
// to sum because it has no side effects and the result isn't used.
// However, inside a b.Loop loop, the compiler ensures function calls
// aren't optimized away.
sum(input)
}
// Outside the loop, the timer is stopped, so we could perform
// cleanup if necessary without affecting the result.
}
func sum(data []int) int {
total := 0
for _, value := range data {
total += value
}
return total
}
func ExampleB_Loop() {
testing.Benchmark(ExBenchmark)
}