File size: 459 Bytes
e36aeda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
go test -bench=Foo -cpuprofile=default.pgo
go test -bench=Foo -pgo=default.pgo
! stdout 'FAIL'

-- main_test.go --
package main

import (
	"testing"
)

var a int

func save(x int) {
	a = x
}

func foo() {
	for i := range yield1 {
		defer save(i)
	}
}

func yield1(yield func(int) bool) {
	yield(1)
}

func BenchmarkFoo(b *testing.B) {
	for i := 0; i < b.N; i++ {
		foo()
	}
	if a != 1 {
		b.Fatalf("a = %d; want 1", a)
	}
}

-- go.mod --
module demo

go 1.24