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

@ -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)
}
}