-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathslices_bench_test.go
More file actions
79 lines (70 loc) · 1.2 KB
/
slices_bench_test.go
File metadata and controls
79 lines (70 loc) · 1.2 KB
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
package slices
import (
"fmt"
"testing"
)
var (
resultSlice []string
resultInt int
s51 = "yyy"
a100 = (func() []string {
a := make([]string, 100)
for i := 0; i < 100; i++ {
if i == 50 {
a[i] = s51
continue
}
a[i] = "x" + fmt.Sprint(i)
}
return a
})()
aa100 = Split(a100, "")
)
func benchmarkMerge(b *testing.B, f func(...[]string) []string) {
var a []string
for i := 0; i < b.N; i++ {
a = f(aa100...)
}
resultSlice = a
}
func BenchmarkMerge(b *testing.B) { benchmarkMerge(b, Merge) }
func benchmarkCount(b *testing.B, f func([]string, string) int) {
var x int
for i := 0; i < b.N; i++ {
x = f(a100, s51)
}
resultInt = x
}
func BenchmarkCount(b *testing.B) { benchmarkCount(b, Count) }
func BenchmarkCount2(b *testing.B) {
benchmarkCount(b,
func(a []string, s string) int {
var n int
for i, j := 0, len(a)-1; i <= j; i, j = i+1, j-1 {
if a[i] == s {
n++
}
if i == j {
break
}
if a[j] == s {
n++
}
}
return n
})
}
func BenchmarkCount3(b *testing.B) {
benchmarkCount(b,
func(a []string, s string) int {
n := 0
for {
i := Index(a, s)
if i == -1 {
return n
}
n++
a = a[i+1:]
}
})
}