-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastrandom_test.go
123 lines (108 loc) · 2.46 KB
/
fastrandom_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
package fastrandom
import (
"fmt"
"math/rand"
"testing"
)
import "github.com/dgryski/go-pcgr"
var p = pcgr.Rand{0x0ddc0ffeebadf00d, 0xcafebabe}
func testFastShortShuffle(t *testing.T, key uint32, N uint) {
for i := N; i >= 3; i-- {
mn := precomputedMagicNumber[i-3]
fa := precomputedFactorial[i-3]
divresult := fastDiv(key, &mn) // value in [0,i)
expresult := key / fa
if divresult != expresult {
t.Error("Expected ", expresult, ", got ", divresult)
}
key = key - divresult*fa // value in [0,(i-1)!)
}
}
func TestFastDiv(t *testing.T) {
N := uint(11)
m := precomputedFactorial[N-2] - 1
for ; m > 0; m-- {
testFastShortShuffle(t, m, N)
}
}
func toId(vals []uint, N uint) uint {
v := uint(0)
for i := uint(0); i < N; i++ {
v = v*N + vals[i]
}
return v
}
func TestFastShuffle(t *testing.T) {
N := uint(5)
x := make(map[uint]bool)
m := precomputedFactorial[N-2]
for mm := uint32(0); mm < m; mm++ {
sh := FastShortShuffle(mm, N)
fmt.Println(sh)
x[toId(sh, N)] = true
}
if uint32(len(x)) != precomputedFactorial[N-2] {
t.Error("Expected ", precomputedFactorial[N-2], ", got ", len(x))
}
}
//go test -bench=.
// standard
func benchmarkGo(b *testing.B, r uint32) {
b.StopTimer()
array := make([]int, r, r)
for i := 0; i < int(r); i++ {
array[i] = i
}
b.StartTimer()
for j := 0; j < b.N; j++ {
for i := r; i > 0; i-- {
idx := rand.Int31n(int32(i))
tmp := array[idx]
array[idx] = array[i-1]
array[i-1] = tmp
}
}
}
// our fast approach that avoids division
func benchmarkFastPCG_dgryski(b *testing.B, r uint32) {
b.StopTimer()
array := make([]int, r, r)
for i := 0; i < int(r); i++ {
array[i] = i
}
b.StartTimer()
for j := 0; j < b.N; j++ {
for i := r; i > 0; i-- {
idx := Randuint32pcg_dgryski(uint32(i), &p)
tmp := array[idx]
array[idx] = array[i-1]
array[i-1] = tmp
}
}
}
// standard PCG
func benchmarkPCG_dgryski(b *testing.B, r uint32) {
b.StopTimer()
array := make([]int, r, r)
for i := 0; i < int(r); i++ {
array[i] = i
}
b.StartTimer()
for j := 0; j < b.N; j++ {
for i := r; i > 0; i-- {
idx := p.Bound(uint32(i))
tmp := array[idx]
array[idx] = array[i-1]
array[i-1] = tmp
}
}
}
func BenchmarkStandardShuffleWithGo1000(b *testing.B) {
benchmarkGo(b, 1000)
}
func BenchmarkStandardShuffleWithPCGWithDivision1000_dgryski(b *testing.B) {
benchmarkPCG_dgryski(b, 1000)
}
func BenchmarkStandardShuffleWithPCGButNoDivision1000_dgryski(b *testing.B) {
benchmarkFastPCG_dgryski(b, 1000)
}