Skip to content

Commit

Permalink
Rewrite benchmarks not to use indirect calls
Browse files Browse the repository at this point in the history
Move the important benchmarks back into the xxhash package itself.

Updates #22
  • Loading branch information
cespare committed Nov 20, 2020
1 parent 9160c38 commit e0ea1e3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
74 changes: 74 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package xxhash

import (
"strings"
"testing"
)

var benchmarks = []struct {
name string
n int64
}{
{"4B", 4},
{"100B", 100},
{"4KB", 4e3},
{"10MB", 10e6},
}

func BenchmarkSum64(b *testing.B) {
for _, bb := range benchmarks {
in := make([]byte, bb.n)
for i := range in {
in[i] = byte(i)
}
b.Run(bb.name, func(b *testing.B) {
b.SetBytes(bb.n)
for i := 0; i < b.N; i++ {
_ = Sum64(in)
}
})
}
}

func BenchmarkSum64String(b *testing.B) {
for _, bb := range benchmarks {
s := strings.Repeat("a", int(bb.n))
b.Run(bb.name, func(b *testing.B) {
b.SetBytes(bb.n)
for i := 0; i < b.N; i++ {
_ = Sum64String(s)
}
})
}
}

func BenchmarkDigestBytes(b *testing.B) {
for _, bb := range benchmarks {
in := make([]byte, bb.n)
for i := range in {
in[i] = byte(i)
}
b.Run(bb.name, func(b *testing.B) {
b.SetBytes(bb.n)
for i := 0; i < b.N; i++ {
h := New()
h.Write(in)
_ = h.Sum64()
}
})
}
}

func BenchmarkDigestString(b *testing.B) {
for _, bb := range benchmarks {
s := strings.Repeat("a", int(bb.n))
b.Run(bb.name, func(b *testing.B) {
b.SetBytes(bb.n)
for i := 0; i < b.N; i++ {
h := New()
h.WriteString(s)
_ = h.Sum64()
}
})
}
}
4 changes: 4 additions & 0 deletions xxhashbench/xxhashbench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"github.com/spaolacci/murmur3"
)

// TODO: The main benchmarks live in the xxhash package now, so the only purpose
// of this is to compare different hash functions. Consider deleting xxhashbench
// or replacing it with a more minimal comparison.

var sink uint64

var benchmarks = []struct {
Expand Down

0 comments on commit e0ea1e3

Please sign in to comment.