Skip to content

Instantly share code, notes, and snippets.

@klauspost
Created December 29, 2022 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klauspost/01d8428905d17c2ad1f66b16f1bd1e2f to your computer and use it in GitHub Desktop.
Save klauspost/01d8428905d17c2ad1f66b16f1bd1e2f to your computer and use it in GitHub Desktop.
package main_test
import (
"crypto/sha1"
"math/rand"
"testing"
"github.com/zeebo/xxh3"
)
func BenchmarkHash8K(b *testing.B) {
var block [8192]byte
r := rand.New(rand.NewSource(1))
r.Read(block[:])
b.Run("sha-1", func(b *testing.B) {
h := sha1.New()
sum := make([]byte, h.Size())
b.SetBytes(int64(len(block)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
h.Reset()
h.Write(block[:])
h.Sum(sum[:0])
}
})
b.Run("xxh3", func(b *testing.B) {
b.SetBytes(int64(len(block)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
xxh3.Hash(block[:])
}
})
b.Run("xxh3-seed", func(b *testing.B) {
b.SetBytes(int64(len(block)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
xxh3.HashSeed(block[:], uint64(i))
}
})
b.Run("xxh128", func(b *testing.B) {
b.SetBytes(int64(len(block)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
xxh3.Hash128(block[:])
}
})
b.Run("xxh128-seed", func(b *testing.B) {
b.SetBytes(int64(len(block)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
xxh3.Hash128Seed(block[:], uint64(i))
}
})
}
@klauspost
Copy link
Author

klauspost commented Dec 29, 2022

cpu: AMD Ryzen 9 3950X 16-Core Processor            
BenchmarkHash8K
BenchmarkHash8K/sha-1-32         	  166794	      7055 ns/op	1161.11 MB/s
BenchmarkHash8K/xxh3-32          	 8696484	       136.3 ns/op	60087.54 MB/s
BenchmarkHash8K/xxh3-seed-32     	 8051788	       148.1 ns/op	55307.44 MB/s
BenchmarkHash8K/xxh128-32        	 8374560	       138.9 ns/op	58997.63 MB/s
BenchmarkHash8K/xxh128-seed-32   	 7799038	       152.3 ns/op	53778.85 MB/s
PASS

Process finished with the exit code 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment