-
Notifications
You must be signed in to change notification settings - Fork 4
/
rand_bench_test.go
115 lines (94 loc) · 1.96 KB
/
rand_bench_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
package main
import (
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"
)
type Rand interface {
Intn(n int) int
}
func benchmarkRand(b *testing.B, r Rand) {
b.ResetTimer()
b.SetParallelism(20)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
r.Intn(4)
r.Intn(10)
r.Intn(20)
r.Intn(1000)
}
})
}
type globalRand struct{}
func (globalRand) Intn(n int) int {
return rand.Intn(n)
}
type roundRobinRand struct {
rands []*rand.Rand
cur int32
}
type lockedSource struct {
lk sync.Mutex
src rand.Source
}
func (r *lockedSource) Int63() (n int64) {
r.lk.Lock()
n = r.src.Int63()
r.lk.Unlock()
return
}
func (r *lockedSource) Seed(seed int64) {
r.lk.Lock()
r.src.Seed(seed)
r.lk.Unlock()
}
func newRoundRobinRand(count int) Rand {
rr := &roundRobinRand{
rands: make([]*rand.Rand, count),
}
for i := 0; i < count; i++ {
rr.rands[i] = rand.New(
&lockedSource{src: rand.NewSource(time.Now().UnixNano())},
)
}
return rr
}
func (r *roundRobinRand) Intn(n int) int {
i := atomic.AddInt32(&r.cur, 1)
return r.rands[int(i)%len(r.rands)].Intn(n)
}
func BenchmarkGlobalRand(b *testing.B) {
benchmarkRand(b, globalRand{})
}
func BenchmarkRoundRobinRand10(b *testing.B) {
benchmarkRand(b, newRoundRobinRand(10))
}
func BenchmarkRoundRobinRand100(b *testing.B) {
benchmarkRand(b, newRoundRobinRand(100))
}
func BenchmarkRandSingle(b *testing.B) {
for i := 0; i < b.N; i++ {
rand.Intn(4)
rand.Intn(10)
rand.Intn(20)
rand.Intn(1000)
}
}
type timeRand struct{}
// Probably not a good idea when n > 10.
func (timeRand) Intn(n int) int {
return int(time.Now().UnixNano()) % n
}
func BenchmarkTimeNowRand(b *testing.B) {
benchmarkRand(b, timeRand{})
}
// Get processor-local counter?
// Atomic operations for processors (faster!)
// Fast rand would
// TODO:
// what if there was a processor-local atomic?
// basically, you get returned the processor ID.
// and you can manage the array safely.
// maybe it should always increment and return?