-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_test.go
114 lines (104 loc) · 2.03 KB
/
max_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package max
import (
"math/rand"
"strconv"
"testing"
)
func if_max_min(values []int) (int, int) {
minV := values[0]
maxV := values[0]
for _, v := range values[1:] {
if v > maxV {
maxV = v
continue // generate branch instruction
}
if v < minV {
minV = v
continue // generate branch instruction
}
}
return minV, maxV
}
func cheat_min(values []int) (int, int) {
minV := values[0]
for _, v := range values[1:] {
minV = v
}
return minV, 0
}
func if_min(values []int) (int, int) {
minV := values[0]
for _, v := range values[1:] {
if v < minV {
minV = v
continue // no effect
}
}
return minV, 0
}
func if_max(values []int) (int, int) {
maxV := values[0]
for _, v := range values[1:] {
if v > maxV {
maxV = v
continue // no effect
}
}
return 0, maxV
}
func cheat_max(values []int) (int, int) {
maxV := values[0]
for _, v := range values[1:] {
maxV = v
}
return 0, maxV
}
func if_pair(values []int) (int, int) {
minV := values[0]
maxV := values[0]
for _, v := range values[1:] {
if v < minV {
minV = v // no branching
}
if v > maxV {
maxV = v // no branching
}
}
return minV, maxV
}
var Sink1, Sink2 int
func BenchmarkFn(b *testing.B) {
benchData := []struct {
name string
fn func([]int) (int, int)
}{
{name: "if_max_min", fn: if_max_min},
{name: "if_max", fn: if_max},
//{name: "cheat_max", fn: cheat_max},
//{name: "if_min", fn: if_min},
//{name: "cheat_min", fn: cheat_min},
//{name: "if_pair", fn: if_pair},
}
values := make([]int, 0, 1e5)
//newMin := 0
//newMax := 1
for i := 0; i < cap(values); i++ {
//values = append(values, newMin)
//newMin--
//values = append(values, newMax)
//newMax++
values = append(values, rand.Int())
}
b.ResetTimer()
for _, l := range []int{ /*1, 5, 15, 20, 1e2,*/ 1e5} {
b.Run("len="+strconv.Itoa(l), func(b *testing.B) {
for _, bench := range benchData {
b.Run("name="+bench.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
Sink1, Sink2 = bench.fn(values[:l])
}
})
}
})
}
}