From 5cefd2a941981a41a13823202ba417960c3bb67b Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 15 Dec 2009 17:21:34 -0800 Subject: rand: add explicit Int31n to avoid 64-bit divide on 32-bit machines use Int31n in Intn when possible. Fixes issue 390. (using 8g) Intn1000 50000000 38 ns/op Int31n1000 50000000 39 ns/op Int63n1000 20000000 114 ns/op R=r CC=golang-dev, skybrian http://codereview.appspot.com/180054 --- src/pkg/rand/rand_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/pkg/rand/rand_test.go') diff --git a/src/pkg/rand/rand_test.go b/src/pkg/rand/rand_test.go index b90c69db7..786831517 100644 --- a/src/pkg/rand/rand_test.go +++ b/src/pkg/rand/rand_test.go @@ -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) + } +} -- cgit v1.2.3