]> Cypherpunks repositories - gostls13.git/commit
math/rand: minor optimization to Perm
authorJosh Bleecher Snyder <josharian@gmail.com>
Tue, 17 Dec 2013 02:49:34 +0000 (13:49 +1100)
committerAndrew Gerrand <adg@golang.org>
Tue, 17 Dec 2013 02:49:34 +0000 (13:49 +1100)
commit4a18e0edd94d156ebbccead44b553e2a436df5f5
treece0fb7fb36ff6f0e6f5f9534baca663ded169e30
parent1561230ca02e6e71afbf5f524fa89a4a5e3fab9a
math/rand: minor optimization to Perm

Instead of writing out 0..n and then reading it
back, just use i when it is needed.

Wikipedia calls this the "inside-out" implementation:
http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

This yields identical values to the previous
implementation, given the same seed. (Note that the
output from Example_rand is unchanged.)

2.8 GHz Intel Core i7, results very stable:

benchmark          old ns/op    new ns/op    delta
BenchmarkPerm3           138          136   -1.45%
BenchmarkPerm30          825          803   -2.67%

Stock Raspberry Pi, minimum improvement out of three runs:

benchmark          old ns/op    new ns/op    delta
BenchmarkPerm3          5774         5664   -1.91%
BenchmarkPerm30        32582        29381   -9.82%

R=golang-dev, dave, mtj, adg
CC=golang-dev
https://golang.org/cl/21030043
src/pkg/math/rand/rand.go
src/pkg/math/rand/rand_test.go