-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbench_test.go
76 lines (66 loc) · 1.3 KB
/
bench_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
package it_test
import (
"slices"
"testing"
"github.com/gomoni/it/islices"
)
const size = 1024 * 1024
var in1M []int
func init() {
in1M = make([]int, size)
for idx := range in1M {
in1M[idx] = idx
}
}
// BenchmarkRange benchmarks the for range loop over a slice
func BenchmarkRange(b *testing.B) {
for range b.N {
cnt := 0
for _, value := range in1M {
cnt += value
}
}
}
// BenchmarkRangeAll benchmarks the slices.All
func BenchmarkRangeAll(b *testing.B) {
for range b.N {
cnt := 0
for _, value := range slices.All(in1M) {
cnt += value
}
}
}
// BenchmarkRangeValues benchmarks the slices.Values
func BenchmarkRangeValues(b *testing.B) {
for range b.N {
cnt := 0
for value := range slices.Values(in1M) {
cnt += value
}
}
}
// BenchmarkRangeAll benchmarks a range loop skipping the odd numbers
func BenchmarkRangeEven(b *testing.B) {
for range b.N {
cnt := 0
for _, value := range in1M {
if value%2 != 0 {
continue
}
cnt += value
}
}
}
// BenchmarkRangeFilterEven uses a Filter method on a sequence to do the filtering
func BenchmarkRangeValuesFilterEven(b *testing.B) {
for range b.N {
cnt := 0
all := slices.Values(in1M)
evens := islices.Filter(all, func(value int) bool {
return value%2 == 0
})
for value := range evens {
cnt += value
}
}
}