-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfun_go_bench_test.go
143 lines (112 loc) · 3.09 KB
/
fun_go_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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package fungo
/*
Software Engineer: Fachrin Aulia Nasution
An always in beta software engineer
*/
import (
"fmt"
"math/rand"
"testing"
)
const (
maxChildren = 1000000
)
type child struct {
Name string
Age int64
Hobbies []string
}
func generateChildrenList(maxData int) []child {
var listOfChildren = make([]child, 0)
for i := 0; i < maxData; i++ {
listOfChildren = append(listOfChildren, child{
Name: "Fachrin",
Age: rand.Int63n(100),
Hobbies: []string{"Guitar", "Software Engineering", "Reading"},
})
}
return listOfChildren
}
func getFilteredChildrenNormalAgeMoreThan50(childrenList []child) []child {
childrenSlice := make([]child, 0)
for _, child := range childrenList {
if child.Age > 50 {
childrenSlice = append(childrenSlice, child)
}
}
return childrenSlice
}
func normalForLoopChildren(childrenList []child) {
for _, child := range childrenList {
if child.Age > 0 {
}
}
}
func getMappedChildrenNameAndHobby(childrenList []child) []string {
var nameWithFirstHobby = make([]string, 0)
for _, child := range childrenList {
nameWithFirstHobby = append(nameWithFirstHobby, fmt.Sprint(
child.Name,
child.Hobbies[0],
))
}
return nameWithFirstHobby
}
func getReducedChildrenAge(childrenList []child) int64 {
var accumulator = int64(0)
for _, child := range childrenList {
accumulator += child.Age
}
return accumulator
}
func BenchmarkFunctionalIteratorMap(b *testing.B) {
childrenList := generateChildrenList(maxChildren)
childrenSlice := NewSlice(childrenList)
b.Run("functionalIterator", func(b *testing.B) {
_ = childrenSlice.Map([]string{}, func(value interface{}, index int) interface{} {
child := value.(child)
return fmt.Sprint(child.Name, child.Hobbies[0])
}).([]string)
})
b.Run("normalIterator", func(b *testing.B) {
_ = getMappedChildrenNameAndHobby(childrenList)
})
}
func BenchmarkFunctionalIteratorFilter(b *testing.B) {
childrenList := generateChildrenList(maxChildren)
childrenSlice := NewSlice(childrenList)
b.Run("functionalIterator", func(b *testing.B) {
_ = childrenSlice.Filter(func(value interface{}, index int) bool {
child := value.(child)
return child.Age > 50
}).([]child)
})
b.Run("normalIterator", func(b *testing.B) {
_ = getFilteredChildrenNormalAgeMoreThan50(childrenList)
})
}
func BenchmarkFunctionalIteratorForEach(b *testing.B) {
childrenList := generateChildrenList(maxChildren)
childrenSlice := NewSlice(childrenList)
b.Run("functionalIterator", func(b *testing.B) {
childrenSlice.ForEach(func(value interface{}, index int) {
if value.(child).Age > 0 {
}
})
})
b.Run("normalIterator", func(b *testing.B) {
normalForLoopChildren(childrenList)
})
}
func BenchmarkFunctionalIteratorReduce(b *testing.B) {
childrenList := generateChildrenList(maxChildren)
childrenSlice := NewSlice(childrenList)
b.Run("functionalIterator", func(b *testing.B) {
_ = childrenSlice.Reduce(func(accumulator int64, value interface{}) int64 {
return accumulator + value.(child).Age
})
})
b.Run("normalIterator", func(b *testing.B) {
_ = getReducedChildrenAge(childrenList)
})
}