-
Notifications
You must be signed in to change notification settings - Fork 24
/
prealloc_test.go
159 lines (139 loc) · 3.34 KB
/
prealloc_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"go/token"
"testing"
"github.com/alexkohler/prealloc/pkg"
)
func Test_checkForPreallocations(t *testing.T) {
const filename = "testdata/sample.go"
fset := token.NewFileSet()
got, err := checkForPreallocations([]string{filename}, fset, true, true, true)
if err != nil {
t.Fatal(err)
}
want := []pkg.Hint{
pkg.Hint{
Pos: 63,
DeclaredSliceName: "y",
},
pkg.Hint{
Pos: 77,
DeclaredSliceName: "z",
},
pkg.Hint{
Pos: 102,
DeclaredSliceName: "t",
},
}
if len(got) != len(want) {
t.Fatalf("expected %d hints, but got %d: %+v", len(want), len(got), got)
}
for i := range got {
act, exp := got[i], want[i]
file := fset.File(act.Pos)
if file.Name() != filename {
t.Errorf("wrong hints[%d].Filename: %q (expected: %q)", i, file.Name(), filename)
}
actLineNumber := file.Position(act.Pos).Line
expLineNumber := file.Position(exp.Pos).Line
if actLineNumber != expLineNumber {
t.Errorf("wrong hints[%d].LineNumber: %d (expected: %d)", i, actLineNumber, expLineNumber)
}
if act.DeclaredSliceName != exp.DeclaredSliceName {
t.Errorf("wrong hints[%d].DeclaredSliceName: %q (expected: %q)", i, act.DeclaredSliceName, exp.DeclaredSliceName)
}
}
}
func BenchmarkSize10NoPreallocate(b *testing.B) {
existing := make([]int64, 10, 10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Don't preallocate our initial slice
var init []int64
for _, element := range existing {
init = append(init, element)
}
}
}
func BenchmarkSize10Preallocate(b *testing.B) {
existing := make([]int64, 10, 10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Preallocate our initial slice
init := make([]int64, 0, len(existing))
for _, element := range existing {
init = append(init, element)
}
}
}
func BenchmarkSize10PreallocateCopy(b *testing.B) {
existing := make([]int64, 10, 10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Preallocate our initial slice
init := make([]int64, len(existing))
copy(init, existing)
}
}
func BenchmarkSize200NoPreallocate(b *testing.B) {
existing := make([]int64, 200, 200)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Don't preallocate our initial slice
var init []int64
for _, element := range existing {
init = append(init, element)
}
}
}
func BenchmarkSize200Preallocate(b *testing.B) {
existing := make([]int64, 200, 200)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Preallocate our initial slice
init := make([]int64, 0, len(existing))
for _, element := range existing {
init = append(init, element)
}
}
}
func BenchmarkSize200PreallocateCopy(b *testing.B) {
existing := make([]int64, 200, 200)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Preallocate our initial slice
init := make([]int64, len(existing))
copy(init, existing)
}
}
func BenchmarkMap(b *testing.B) {
benchmarks := []struct {
size int
preallocate bool
}{
{10, false},
{10, true},
{200, false},
{200, true},
}
var m map[int]int
for _, bm := range benchmarks {
no := ""
if !bm.preallocate {
no = "No"
}
b.Run(fmt.Sprintf("Size%d%sPreallocate", bm.size, no), func(b *testing.B) {
for i := 0; i < b.N; i++ {
if bm.preallocate {
m = make(map[int]int, bm.size)
} else {
m = make(map[int]int)
}
for j := 0; j < bm.size; j++ {
m[j] = j
}
}
})
}
}