This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
/
x_test.go
138 lines (119 loc) · 2.63 KB
/
x_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
package x
import (
"math/rand"
"sort"
"testing"
"time"
)
func TestMergeInt(t *testing.T) {
l := SortedInt(10)
if !sort.IsSorted(int64arr(l)) {
t.Errorf("Not sorted: [%v]\n", l)
t.FailNow()
}
for i := 0; i < 50000; i++ {
l = mergeInt(l, rand.Int63())
if !sort.IsSorted(int64arr(l)) {
t.Errorf("Not sorted: [%v]\n", l)
t.FailNow()
}
}
}
func TestMergeString(t *testing.T) {
l := SortedString(10)
for !sort.StringsAreSorted(l) {
t.Errorf("Not sorted: [%v]\n", l)
}
for i := 0; i < 5000; i++ {
s := UniqueString(rand.Intn(11))
l = mergeString(l, s)
if !sort.StringsAreSorted(l) {
t.Error("Strings are not sorted")
t.FailNow()
}
}
}
// Around 0.37 ns/op on my laptop.
// This is 25x faster than string comparisons, so iterating over and merging
// lists of ints would be a lot faster than doing the same for strings.
func BenchmarkInt64(b *testing.B) {
rand.Seed(time.Now().UnixNano())
var m, n int64
m = rand.Int63()
n = rand.Int63()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = m == n
}
}
// There's no difference between this and int64 benchmarks.
func BenchmarkInt32(b *testing.B) {
rand.Seed(time.Now().UnixNano())
var m, n int32
m = rand.Int31()
n = rand.Int31()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = m == n
}
}
func BenchmarkFloat(b *testing.B) {
rand.Seed(time.Now().UnixNano())
var m, n float64
m = rand.Float64()
n = rand.Float64()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = m == n
}
}
// Around 8.67 ns/op on my laptop.
func BenchmarkString(b *testing.B) {
rand.Seed(time.Now().UnixNano())
var m, n string
l := rand.Intn(11) // Num permutations are now 5x +ve vals for int64
m = UniqueString(l)
n = UniqueString(l)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = m == n
}
}
func BenchmarkMergeSortedInt(b *testing.B) {
rand.Seed(time.Now().UnixNano())
sz := rand.Intn(50000) + 1000
l := SortedInt(sz)
b.ResetTimer()
for i := 0; i < b.N; i++ {
l = mergeInt(l, rand.Int63())
}
}
func BenchmarkMergeSortedString(b *testing.B) {
rand.Seed(time.Now().UnixNano())
sz := rand.Intn(50000) + 1000
l := SortedString(sz)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := UniqueString(rand.Intn(11))
l = mergeString(l, s)
}
}
func BenchmarkFindIndexInt(b *testing.B) {
rand.Seed(time.Now().UnixNano())
sz := rand.Intn(50000) + 1000
l := SortedInt(sz)
b.ResetTimer()
for i := 0; i < b.N; i++ {
findIndexInt(l, rand.Int63())
}
}
func BenchmarkFindIndexString(b *testing.B) {
rand.Seed(time.Now().UnixNano())
sz := rand.Intn(50000) + 1000
l := SortedString(sz)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := UniqueString(rand.Intn(11))
findIndexString(l, s)
}
}