-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathopt_away_test.go
59 lines (47 loc) · 1.09 KB
/
opt_away_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
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) Efficient Go Authors
// Licensed under the Apache License 2.0.
package compileroptimizeaway
import (
"math"
"runtime"
"testing"
)
// BenchmarkPopcnt_Wrong is an example microbenchmark that can be optimized by compiler.
// Read more in "Efficient Go"; Example 8-16.
func BenchmarkPopcnt_Wrong(b *testing.B) {
for i := 0; i < b.N; i++ {
popcnt(math.MaxUint64)
}
}
func BenchmarkPopcnt_Wrong2(b *testing.B) {
for i := 0; i < b.N; i++ {
popcnt(Input)
}
}
var Sink uint64
func BenchmarkPopcnt_Wrong3(b *testing.B) {
var r uint64
b.ResetTimer()
for i := 0; i < b.N; i++ {
r = popcnt(math.MaxUint64)
}
Sink = r
}
// BenchmarkPopcnt_Sink is one example on how we can countermeasure the problem visible in BenchmarkPopcnt_Wrong.
// Read more in "Efficient Go"; Example 8-18.
func BenchmarkPopcnt_Sink(b *testing.B) {
var r uint64
b.ResetTimer()
for i := 0; i < b.N; i++ {
r = popcnt(Input)
}
Sink = r
}
func BenchmarkPopcnt_KeepAlive(b *testing.B) {
var r uint64
for i := 0; i < b.N; i++ {
r = popcnt(Input)
}
runtime.KeepAlive(r)
}
var Input uint64 = math.MaxUint64