Skip to content

Commit

Permalink
Make benchmarks more consistent across invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Jan 26, 2024
1 parent 7a161ad commit 1403cdc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
6 changes: 3 additions & 3 deletions v2/benchmark/BWT_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"math/rand"
"testing"
"time"

kanzi "github.com/flanglet/kanzi-go/v2"
"github.com/flanglet/kanzi-go/v2/transform"
Expand Down Expand Up @@ -50,8 +49,9 @@ func testBWTSpeed(isBWT bool, iter, size int) error {
buf3 := make([]byte, size)

for jj := 0; jj < 3; jj++ {
// Initialize with a fixed seed to get consistent results
r := rand.New(rand.NewSource(int64(jj * 1234567)))
var bwt kanzi.ByteTransform
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))

for i := 0; i < iter; i++ {
if isBWT {
Expand All @@ -61,7 +61,7 @@ func testBWTSpeed(isBWT bool, iter, size int) error {
}

for i := range buf1 {
buf1[i] = byte(rnd.Intn(255) + 1)
buf1[i] = byte(r.Intn(255) + 1)
}

_, _, err1 := bwt.Forward(buf1, buf2)
Expand Down
4 changes: 3 additions & 1 deletion v2/benchmark/Entropy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func getDecoder(name string, ibs kanzi.InputBitStream) kanzi.EntropyDecoder {
}

func testEntropySpeed(b *testing.B, name string) error {
// Initialize with a fixed seed to get consistent results
r := rand.New(rand.NewSource(1234567))
repeats := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3}

for jj := 0; jj < 3; jj++ {
Expand All @@ -122,7 +124,7 @@ func testEntropySpeed(b *testing.B, name string) error {

length := repeats[idx]
idx = (idx + 1) & 0x0F
b := byte(rand.Intn(256))
b := byte(r.Intn(256))

if i0+length >= size {
length = size - i0 - 1
Expand Down
4 changes: 3 additions & 1 deletion v2/benchmark/Transforms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func BenchmarkMTFT(b *testing.B) {
}

func testTransformSpeed(name string, iter int) error {
// Initialize with a fixed seed to get consistent results
r := rand.New(rand.NewSource(1234567))
size := 50000

for jj := 0; jj < 3; jj++ {
Expand All @@ -165,7 +167,7 @@ func testTransformSpeed(name string, iter int) error {
n := iter / 20

for n < len(input) {
val := byte(rand.Intn(64))
val := byte(r.Intn(64))

if val%7 == 0 {
val = 0
Expand Down

0 comments on commit 1403cdc

Please sign in to comment.