forked from samber/lo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
62 lines (50 loc) · 1.09 KB
/
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
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 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
}
}
})
}