-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_test.go
218 lines (202 loc) · 3.33 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package goset
import "testing"
type Poem struct {
Title string
Body []string
}
type Poet struct {
Age int
Name string
Poems []*Poem
}
func IsSamePoet(a Poet, b Poet) bool {
if a.Age != b.Age {
return false
}
if a.Name != b.Name {
return false
}
if len(a.Poems) != len(b.Name) {
return false
}
return IsEqual(a.Poems, b.Poems)
}
func UniqPoets(poets []Poet) (r []Poet) {
for _, p := range poets {
found := false
for _, added := range r {
if IsSamePoet(p, added) {
found = true
}
}
if found {
continue
}
r = append(r, p)
}
return
}
var LiBai = Poet{
69,
"李白",
[]*Poem{
&Poem{
"望庐山瀑布",
[]string{
"日照香炉生紫烟",
"遥看瀑布挂前川",
"飞流直下三千尺",
"疑是银河落九天",
},
},
&Poem{
"夜宿山寺",
[]string{
"危楼高百尺",
"手可摘星辰",
"不敢高声语",
"恐惊天上人",
},
},
&Poem{
"关山月",
[]string{
"明月出天山",
"苍茫云海间",
"长风几万里",
"吹度玉门关",
"汉下白登道",
"胡窥青海湾",
"由来征战日",
"不见有人还",
},
},
},
}
var DuFu = Poet{
57,
"杜甫",
[]*Poem{
&Poem{
"春望",
[]string{
"国破山河在",
"城春草木深",
"感时花溅泪",
"恨别鸟惊心",
"烽火连三月",
"家书抵万金",
"白头搔更短",
"浑欲不胜簪",
},
},
&Poem{
"江南风李龟年",
[]string{
"岐王宅里寻常见",
"崔九堂前几度闻",
"正是江南好风景",
"落花时节又逢君",
},
},
},
}
var CuiHu = Poet{
29,
"崔护",
[]*Poem{
&Poem{
"题南庄",
[]string{
"去年今日此门中",
"人面桃花相映红",
"人面不知何处去",
"桃花依旧笑春风",
},
},
},
}
var SuShi = Poet{
40,
"苏轼",
[]*Poem{
&Poem{
"念奴娇",
[]string{
"大江东去浪",
"淘尽千古风流",
"人物故垒是三国",
"周郎赤壁",
},
},
&Poem{
"东坡肉",
[]string{
"东坡肉真的好难吃",
"东坡肉真的好难吃",
"东坡肉真的好难吃",
"东坡肉真的好难吃",
},
},
},
}
var XinQiji = Poet{
52,
"辛弃疾",
[]*Poem{
&Poem{
"郁孤台",
[]string{
"郁孤台下清江水",
"中间多少行人泪",
"西北望长安",
"可怜无数山",
},
},
&Poem{
"破阵子",
[]string{
"鞋儿破",
"帽儿破",
"身上的袈裟破",
},
},
},
}
func BenchmarkDiffernceBuiltinTypes(b *testing.B) {
s1 := []string{"a", "b", "c", "d", "e", "f"}
s2 := []string{"e", "f", "g", "h", "i", "j"}
for i := 0; i < b.N; i++ {
Difference(s1, s2)
}
return
}
func BenchmarkDiffernceCustomTypes(b *testing.B) {
s1 := []Poet{LiBai, DuFu, CuiHu, SuShi}
s2 := []Poet{LiBai, SuShi, XinQiji}
for i := 0; i < b.N; i++ {
Difference(s1, s2)
}
return
}
func BenchmarkUniqBuiltinTypes(b *testing.B) {
s1 := []int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}
for i := 0; i < b.N; i++ {
Uniq(s1)
}
return
}
func BenchmarkUniqCustomTypes(b *testing.B) {
s1 := []Poet{LiBai, DuFu, CuiHu, SuShi, LiBai, SuShi, XinQiji}
for i := 0; i < b.N; i++ {
Uniq(s1)
}
return
}
func BenchmarkUniqPoets(b *testing.B) {
s1 := []Poet{LiBai, DuFu, CuiHu, SuShi, LiBai, SuShi, XinQiji}
for i := 0; i < b.N; i++ {
UniqPoets(s1)
}
return
}