Skip to content

Commit

Permalink
Set new seed to random on every benchmark iteration
Browse files Browse the repository at this point in the history
The benchmarks will be even more reliable.
  • Loading branch information
mrekucci committed Jul 29, 2015
1 parent 6454dc2 commit 3e5a8b4
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 22 deletions.
5 changes: 3 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ const (

// randStr returns a string of length n constructed
// from pseudo-randomly selected characters from t.
func randStr(n int, t string) string {
// The pseudo-randomness uses random values from s.
func randStr(n int, t string, s rand.Source) string {
l := len(t) - 1
chars := make([]byte, n)
if l > 0 {
for i, p := range rand.New(rand.NewSource(1238)).Perm(n) {
for i, p := range rand.New(s).Perm(n) {
chars[i] = t[p%l]
}
}
Expand Down
24 changes: 14 additions & 10 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,33 @@

package epi

import "testing"
import (
"math/rand"
"testing"
)

func TestRandStr(t *testing.T) {
for _, test := range []struct {
n int
t string
s rand.Source
}{
{1, ""},
{1, "a"},
{1, "abc"},
{10, "ab"},
{10, "abcdefghijklmnopqrstuvwxyz"},
{100, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"},
{1, "", rand.NewSource(1)},
{1, "a", rand.NewSource(2)},
{1, "abc", rand.NewSource(3)},
{10, "ab", rand.NewSource(4)},
{10, "abcdefghijklmnopqrstuvwxyz", rand.NewSource(5)},
{100, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", rand.NewSource(6)},
} {
if got := len(randStr(test.n, test.t)); got != test.n {
t.Errorf("len(randStr(%d, %q) = %d; want %d", test.n, test.t, got, test.n)
if got := len(randStr(test.n, test.t, test.s)); got != test.n {
t.Errorf("len(randStr(%d, %q, %v) = %d; want %d", test.n, test.t, test.s, got, test.n)
}
}
}

func benchRandStr(b *testing.B, size int) {
for i := 0; i < b.N; i++ {
randStr(size, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
randStr(size, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", rand.NewSource(int64(i)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion dutchflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func benchRearrange(b *testing.B, size int) {
p := size / 3
q := size
for i := 0; i < b.N; i++ {
data := rand.New(rand.NewSource(1234)).Perm(size)
data := rand.New(rand.NewSource(int64(i))).Perm(size)
j := data[i%size]%(q-p) + p
b.StartTimer()
Rearrange(data, j)
Expand Down
2 changes: 1 addition & 1 deletion maxdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestMinBatteryCap(t *testing.T) {
func benchMinBatteryCap(b *testing.B, size int) {
b.StopTimer()
for i := 0; i < b.N; i++ {
data := rand.New(rand.NewSource(1235)).Perm(size)
data := rand.New(rand.NewSource(int64(i))).Perm(size)
b.StartTimer()
MinBatteryCap(data)
b.StopTimer()
Expand Down
2 changes: 1 addition & 1 deletion nextperm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestNext(t *testing.T) {
func benchNext(b *testing.B, size int) {
b.StopTimer()
for i := 0; i < b.N; i++ {
data := rand.New(rand.NewSource(1236)).Perm(size)
data := rand.New(rand.NewSource(int64(i))).Perm(size)
b.StartTimer()
NextPerm(data)
b.StopTimer()
Expand Down
2 changes: 1 addition & 1 deletion powerset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func benchPowerSet(b *testing.B, size int) {
b.StopTimer()
for i := 0; i < b.N; i++ {
data := make([]interface{}, size)
for j, p := range rand.New(rand.NewSource(1237)).Perm(size) {
for j, p := range rand.New(rand.NewSource(int64(i))).Perm(size) {
data[j] = p
}
b.StartTimer()
Expand Down
7 changes: 5 additions & 2 deletions reversewords_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package epi

import "testing"
import (
"math/rand"
"testing"
)

func TestReverseWords(t *testing.T) {
for _, test := range []struct {
Expand All @@ -31,7 +34,7 @@ func TestReverseWords(t *testing.T) {
func benchReverseWords(b *testing.B, size int) {
b.StopTimer()
for i := 0; i < b.N; i++ {
s := randStr(size, "The quick brown fox jumps over the lazy dog") // Pangram.
s := randStr(size, "The quick brown fox jumps over the lazy dog", rand.NewSource(int64(i))) // Pangram.
b.StartTimer()
ReverseWords(s)
b.StopTimer()
Expand Down
9 changes: 6 additions & 3 deletions rlecompr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package epi

import "testing"
import (
"math/rand"
"testing"
)

func TestRLEEncode(t *testing.T) {
for _, test := range []struct {
Expand Down Expand Up @@ -40,7 +43,7 @@ func TestRLEEncode(t *testing.T) {
func benchRLEEncode(b *testing.B, size int) {
b.StopTimer()
for i := 0; i < b.N; i++ {
s := randStr(size, "abcdefghijklmnopqrstuvwxyz")
s := randStr(size, "abcdefghijklmnopqrstuvwxyz", rand.NewSource(int64(i)))
b.StartTimer()
RLEEncode(s)
b.StopTimer()
Expand Down Expand Up @@ -87,7 +90,7 @@ func TestRLEDecode(t *testing.T) {
func benchRLEDecode(b *testing.B, size int) {
b.StopTimer()
for i := 0; i < b.N; i++ {
s, ok := RLEEncode(randStr(size, "abcdefghijklmnopqrstuvwxyz"))
s, ok := RLEEncode(randStr(size, "abcdefghijklmnopqrstuvwxyz", rand.NewSource(int64(i))))
if !ok {
b.Errorf("RLEEncode did not encode string properly")
}
Expand Down
2 changes: 1 addition & 1 deletion spiralmetrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestClockwise(t *testing.T) {
func benchClockwise(b *testing.B, size int) {
b.StopTimer()
for i := 0; i < b.N; i++ {
ints := rand.New(rand.NewSource(1239)).Perm(size)
ints := rand.New(rand.NewSource(int64(i))).Perm(size)
data := make([][]int, len(ints))
for j := range data {
data[j] = append([]int(nil), ints...)
Expand Down

0 comments on commit 3e5a8b4

Please sign in to comment.