-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathslice_test.go
112 lines (99 loc) · 2.01 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
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
package benchmark
import (
"container/list"
"testing"
)
func BenchmarkSliceReadRange(b *testing.B) {
m := generateIntSlice(b)
for n := 0; n < b.N; n++ {
for i, e := range m {
checkItem(b, i, e)
}
}
}
func BenchmarkSliceReadForward(b *testing.B) {
m := generateIntSlice(b)
for n := 0; n < b.N; n++ {
for i := 0; i < BenchMarkSize; i++ {
e := m[i]
checkItem(b, i, e)
}
}
}
func BenchmarkSliceReadBackwards(b *testing.B) {
m := generateIntSlice(b)
for n := 0; n < b.N; n++ {
for i := BenchMarkSize - 1; i >= 0; i-- {
e := m[i]
checkItem(b, i, e)
}
}
}
func BenchmarkSliceReadLastItemFirst(b *testing.B) {
m := generateIntSlice(b)
for n := 0; n < b.N; n++ {
if m[BenchMarkSize-1] != BenchMarkSize-1 {
b.Fail()
}
for i := 0; i < BenchMarkSize; i++ {
e := m[i]
checkItem(b, i, e)
}
}
}
func BenchmarkSliceFillByIndex(b *testing.B) {
for n := 0; n < b.N; n++ {
var m [BenchMarkSize]int
for i := 0; i < BenchMarkSize; i++ {
m[i] = i
}
}
}
func BenchmarkSliceFillByIndexMake(b *testing.B) {
for n := 0; n < b.N; n++ {
m := make([]int, BenchMarkSize)
for i := 0; i < BenchMarkSize; i++ {
m[i] = i
}
}
}
func BenchmarkSliceFillMakeAppend(b *testing.B) {
for n := 0; n < b.N; n++ {
m := make([]int, 0, BenchMarkSize)
for i := 0; i < BenchMarkSize; i++ {
m = append(m, i)
}
}
}
func BenchmarkSliceFillAppendNoMake(b *testing.B) {
for n := 0; n < b.N; n++ {
var m []int
for i := 0; i < BenchMarkSize; i++ {
m = append(m, i)
}
}
}
func BenchmarkSliceFillSmallMakeAppend(b *testing.B) {
for n := 0; n < b.N; n++ {
m := make([]int, 0, BenchMarkSize/10)
for i := 0; i < BenchMarkSize; i++ {
m = append(m, i)
}
}
}
func BenchmarkFillLinkedListPushBack(b *testing.B) {
for n := 0; n < b.N; n++ {
l := list.New()
for i := 0; i < BenchMarkSize; i++ {
l.PushBack("a")
}
}
}
func BenchmarkFillLinkedListPushFront(b *testing.B) {
for n := 0; n < b.N; n++ {
l := list.New()
for i := 0; i < BenchMarkSize; i++ {
l.PushFront("a")
}
}
}