-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_benchmark_test.go
124 lines (101 loc) · 2.29 KB
/
map_benchmark_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
113
114
115
116
117
118
119
120
121
122
123
124
package lo
import (
"math/rand"
"strconv"
"testing"
"time"
lop "github.com/samber/lo/parallel"
"github.com/thoas/go-funk"
)
func sliceGenerator(size uint) []int64 {
r := rand.New(rand.NewSource(time.Now().Unix()))
result := make([]int64, size)
for i := uint(0); i < size; i++ {
result[i] = r.Int63()
}
return result
}
func mapGenerator(size uint) map[int64]int64 {
r := rand.New(rand.NewSource(time.Now().Unix()))
result := make(map[int64]int64, size)
for i := uint(0); i < size; i++ {
result[int64(i)] = r.Int63()
}
return result
}
func BenchmarkMap(b *testing.B) {
arr := sliceGenerator(1000000)
b.Run("lo.Map", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = Map(arr, func(x int64, i int) string {
return strconv.FormatInt(x, 10)
})
}
})
b.Run("lop.Map", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = lop.Map(arr, func(x int64, i int) string {
return strconv.FormatInt(x, 10)
})
}
})
b.Run("reflect", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = funk.Map(arr, func(x int64) string {
return strconv.FormatInt(x, 10)
})
}
})
b.Run("for", func(b *testing.B) {
for n := 0; n < b.N; n++ {
results := make([]string, len(arr))
for i, item := range arr {
result := strconv.FormatInt(item, 10)
results[i] = result
}
}
})
}
// also apply to UniqValues
func BenchmarkUniqKeys(b *testing.B) {
m := []map[int64]int64{
mapGenerator(100000),
mapGenerator(100000),
mapGenerator(100000),
}
// allocate just in time + ordered
b.Run("lo.UniqKeys.jit-alloc", func(b *testing.B) {
for n := 0; n < b.N; n++ {
seen := make(map[int64]struct{})
result := make([]int64, 0)
for i := range m {
for k := range m[i] {
if _, exists := seen[k]; exists {
continue
}
seen[k] = struct{}{}
result = append(result, k) //nolint:staticcheck
}
}
}
})
// preallocate + unordered
b.Run("lo.UniqKeys.preallocate", func(b *testing.B) {
for n := 0; n < b.N; n++ {
size := 0
for i := range m {
size += len(m[i])
}
seen := make(map[int64]struct{}, size)
for i := range m {
for k := range m[i] {
seen[k] = struct{}{}
}
}
result := make([]int64, 0, len(seen))
for k := range seen {
result = append(result, k) //nolint:staticcheck
}
}
})
}