forked from Allenxuxu/gev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_load_balance_test.go
63 lines (52 loc) · 1003 Bytes
/
benchmark_load_balance_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
package gev
import (
"container/heap"
"math/rand"
"testing"
)
type IntHeap []int
func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}
func (h *IntHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
func testData() IntHeap {
n := 10000
testData := IntHeap(make([]int, 0, n))
for i := 0; i < n; i++ {
testData = append(testData, rand.Intn(10000))
}
return testData
}
func BenchmarkHeap(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
data := testData()
heap.Init(&data)
heap.Pop(&data)
}
}
func min(data IntHeap) int {
ret := data[0]
for _, v := range data {
if v < ret {
ret = v
}
}
return ret
}
func BenchmarkMin(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
data := testData()
min(data)
}
}