-
Notifications
You must be signed in to change notification settings - Fork 9
/
benchmark_test.go
97 lines (91 loc) · 1.7 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
package deque
import (
"container/list"
"fmt"
"math/rand"
"reflect"
"regexp"
"strings"
"testing"
)
func benchNameSuffix() string {
var elem Elem
t := reflect.TypeOf(elem)
if t == nil {
return ""
}
rex1 := regexp.MustCompile(`\w+\.`)
str1 := rex1.ReplaceAllString(t.String(), "")
str2 := strings.ReplaceAll(str1, "interface {}", "interface{}")
str3 := fmt.Sprintf("<%s>", str2)
return str3
}
func BenchmarkPushBack(b *testing.B) {
b.Run("Deque"+benchNameSuffix(), func(b *testing.B) {
dq := NewDeque()
for i := 0; i < b.N; i++ {
dq.PushBack(Elem(i))
}
})
b.Run("list.List", func(b *testing.B) {
lst := list.New()
for i := 0; i < b.N; i++ {
lst.PushBack(i)
}
})
}
func BenchmarkPushFront(b *testing.B) {
b.Run("Deque"+benchNameSuffix(), func(b *testing.B) {
dq := NewDeque()
for i := 0; i < b.N; i++ {
dq.PushFront(Elem(i))
}
})
b.Run("list.List", func(b *testing.B) {
lst := list.New()
for i := 0; i < b.N; i++ {
lst.PushFront(i)
}
})
}
func BenchmarkRandom(b *testing.B) {
const na = 100000
a := make([]int, na)
for i := 0; i < na; i++ {
a[i] = rand.Int()
}
b.Run("Deque"+benchNameSuffix(), func(b *testing.B) {
dq := NewDeque()
for i := 0; i < b.N; i++ {
switch a[i%na] % 4 {
case 0:
dq.PushBack(Elem(i))
case 1:
dq.PushFront(Elem(i))
case 2:
dq.PopBack()
case 3:
dq.PopFront()
}
}
})
b.Run("list.List", func(b *testing.B) {
lst := list.New()
for i := 0; i < b.N; i++ {
switch a[i%na] % 4 {
case 0:
lst.PushBack(i)
case 1:
lst.PushFront(i)
case 2:
if v := lst.Back(); v != nil {
lst.Remove(v)
}
case 3:
if v := lst.Front(); v != nil {
lst.Remove(v)
}
}
}
})
}