-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice_test.go
47 lines (37 loc) · 1.25 KB
/
slice_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
package main
import (
"testing"
)
// go test -bench . -benchmem
// BenchmarkEscapeWithAppend-16 1000000000 0.1479 ns/op 0 B/op 0 allocs/op
func BenchmarkEscapeWithAppend(b *testing.B) {
data := make([]*bigStruct, 0, numberOfItems)
element := bigStruct{
A: 123, B: 456, C: 789,
D: "ABC", E: "DEF", F: "HIJ",
G: true, H: true, I: true,
}
for i := 0; i < numberOfItems; i++ {
data = append(data, &element)
}
}
// BenchmarkEscapeWithIndex-16 1000000000 0.07195 ns/op 0 B/op 0 allocs/op
func BenchmarkEscapeWithIndex(b *testing.B) {
data := make([]*bigStruct, numberOfItems)
element := bigStruct{
A: 123, B: 456, C: 789,
D: "ABC", E: "DEF", F: "HIJ",
G: true, H: true, I: true,
}
for i := 0; i < numberOfItems; i++ {
data[i] = &element
}
}
// BenchmarkEscapeWithAppendAndReturn-16 1000000000 0.1521 ns/op 0 B/op 0 allocs/op
func BenchmarkEscapeWithAppendAndReturn(b *testing.B) {
_ = fillSliceWithAppend()
}
// BenchmarkEscapeWithIndexAndReturn-16 1000000000 0.05926 ns/op 0 B/op 0 allocs/op
func BenchmarkEscapeWithIndexAndReturn(b *testing.B) {
_ = fillSliceWithIndex()
}