-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbench_test.go
More file actions
102 lines (97 loc) · 2.3 KB
/
bench_test.go
File metadata and controls
102 lines (97 loc) · 2.3 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package iter
import (
"fmt"
"reflect"
"testing"
)
func BenchmarkIteration(b *testing.B) {
for x := uint64(4); x < 18; x += 4 {
benchCount := 1 << (x - 1)
b.Run(fmt.Sprintf("Slices/%d", benchCount), func(b *testing.B) {
benchStrings := make([]string, benchCount)
for y := 0; y < benchCount; y++ {
benchStrings[y] = fmt.Sprintf("bench string %v", y)
}
benchVal := reflect.ValueOf(benchStrings)
benchVals := make([]reflect.Value, len(benchStrings))
for i, v := range benchStrings {
benchVals[i] = reflect.ValueOf(v)
}
b.ResetTimer()
b.Run("Range", func(b *testing.B) {
f := func(idx int, v reflect.Value) error {
return nil
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for z, v := range benchVals {
f(z, v)
}
}
})
b.Run("Iter", func(b *testing.B) {
it := &Iter{}
f := func(idx int, v reflect.Value) error {
return nil
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
it.IterSlice(benchVal, f)
}
})
b.Run("RecoverIter", func(b *testing.B) {
it := recoverIter{&Iter{}}
f := func(idx int, v reflect.Value) error {
return nil
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
it.IterSlice(benchVal, f)
}
})
})
b.Run(fmt.Sprintf("Maps/%d", benchCount), func(b *testing.B) {
benchMaps := make(map[int]string, benchCount)
for y := 0; y < benchCount; y++ {
benchMaps[y] = fmt.Sprintf("bench string %v", y)
}
benchVal := reflect.ValueOf(benchMaps)
benchVals := make([]reflect.Value, len(benchMaps))
for i, v := range benchMaps {
benchVals[i] = reflect.ValueOf(v)
}
b.ResetTimer()
b.Run("Range", func(b *testing.B) {
f := func(k, v reflect.Value) error {
return nil
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for z, v := range benchVals {
f(benchVals[z], v)
}
}
})
b.Run("Iter", func(b *testing.B) {
it := &Iter{}
f := func(k, v reflect.Value) error {
return nil
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
it.IterMap(benchVal, f)
}
})
b.Run("RecoverIter", func(b *testing.B) {
it := recoverIter{&Iter{}}
f := func(idx int, v reflect.Value) error {
return nil
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
it.IterSlice(benchVal, f)
}
})
})
}
}