1
0
mirror of https://github.com/golang/go synced 2024-11-21 21:04:41 -07:00

rand: add explicit Int31n to avoid 64-bit divide on 32-bit machines

use Int31n in Intn when possible.

Fixes #390.

(using 8g)
Intn1000      50000000         38 ns/op
Int31n1000    50000000         39 ns/op
Int63n1000    20000000        114 ns/op

R=r
CC=golang-dev, skybrian
https://golang.org/cl/180054
This commit is contained in:
Russ Cox 2009-12-15 17:21:34 -08:00
parent 1e9e7ec4b3
commit f64bb56bae
2 changed files with 38 additions and 2 deletions

View File

@ -62,10 +62,25 @@ func (r *Rand) Int63n(n int64) int64 {
}
// Int31n returns, as an int32, a non-negative pseudo-random number in [0,n).
func (r *Rand) Int31n(n int32) int32 { return int32(r.Int63n(int64(n))) }
func (r *Rand) Int31n(n int32) int32 {
if n <= 0 {
return 0
}
max := int32((1 << 31) - 1 - (1<<31)%uint32(n))
v := r.Int31()
for v > max {
v = r.Int31()
}
return v % n
}
// Intn returns, as an int, a non-negative pseudo-random number in [0,n).
func (r *Rand) Intn(n int) int { return int(r.Int63n(int64(n))) }
func (r *Rand) Intn(n int) int {
if n <= 1<<31-1 {
return int(r.Int31n(int32(n)))
}
return int(r.Int63n(int64(n)))
}
// Float64 returns, as a float64, a pseudo-random number in [0.0,1.0).
func (r *Rand) Float64() float64 { return float64(r.Int63()) / (1 << 63) }

View File

@ -327,3 +327,24 @@ func BenchmarkInt63Unthreadsafe(b *testing.B) {
r.Int63()
}
}
func BenchmarkIntn1000(b *testing.B) {
r := New(NewSource(1))
for n := b.N; n > 0; n-- {
r.Intn(1000)
}
}
func BenchmarkInt63n1000(b *testing.B) {
r := New(NewSource(1))
for n := b.N; n > 0; n-- {
r.Int63n(1000)
}
}
func BenchmarkInt31n1000(b *testing.B) {
r := New(NewSource(1))
for n := b.N; n > 0; n-- {
r.Int31n(1000)
}
}