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
diff --git a/src/pkg/rand/rand_test.go b/src/pkg/rand/rand_test.go
index b90c69d..7868315 100644
--- a/src/pkg/rand/rand_test.go
+++ b/src/pkg/rand/rand_test.go
@@ -327,3 +327,24 @@
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)
+ }
+}