Skip to content

Instantly share code, notes, and snippets.

@joshcarter
Created December 4, 2022 04:56
Show Gist options
  • Save joshcarter/cf37a8af1825aa4c3f282451ed8ab2a8 to your computer and use it in GitHub Desktop.
Save joshcarter/cf37a8af1825aa4c3f282451ed8ab2a8 to your computer and use it in GitHub Desktop.
Benchmark of various Go hashes
// Tests to benchmark how fast a 128 bytes of data can be turned into
// a 4 byte hash.
package hash_speed
import (
"crypto/sha256"
"crypto/sha512"
"hash/crc32"
"hash/crc64"
"hash/fnv"
"testing"
"github.com/cespare/xxhash/v2"
blake2b "github.com/minio/blake2b-simd"
sha256simd "github.com/minio/sha256-simd"
"golang.org/x/crypto/sha3"
"github.com/spectralogic/go-core/sequence"
)
var dataLen = 1024 * 1024
var letters = sequence.NewLetterSequence(0)
func BenchmarkHashSpeed_Fnv(b *testing.B) {
data := make([]byte, dataLen)
letters.Fill(data)
hash := fnv.New32a()
b.ResetTimer()
for i := 0; i < b.N; i++ {
hash.Write(data)
}
}
func BenchmarkHashSpeed_Sha512(b *testing.B) {
data := make([]byte, dataLen)
letters.Fill(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sha512.Sum512(data)
}
}
func BenchmarkHashSpeed_Sha256(b *testing.B) {
data := make([]byte, dataLen)
letters.Fill(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sha256.Sum256(data)
}
}
func BenchmarkHashSpeed_Sha256Simd(b *testing.B) {
data := make([]byte, dataLen)
letters.Fill(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sha256simd.Sum256(data)
}
}
func BenchmarkHashSpeed_Sha3_256(b *testing.B) {
data := make([]byte, dataLen)
letters.Fill(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sha3.Sum256(data)
}
}
func BenchmarkHashSpeed_Sha3_512(b *testing.B) {
data := make([]byte, dataLen)
letters.Fill(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sha3.Sum512(data)
}
}
func BenchmarkHashSpeed_Crc32C(b *testing.B) {
data := make([]byte, dataLen)
letters.Fill(data)
crc32c := crc32.MakeTable(crc32.Castagnoli)
b.ResetTimer()
for i := 0; i < b.N; i++ {
crc32.Checksum(data, crc32c)
}
}
func BenchmarkHashSpeed_Crc64(b *testing.B) {
data := make([]byte, dataLen)
letters.Fill(data)
table := crc64.MakeTable(crc64.ISO)
b.ResetTimer()
for i := 0; i < b.N; i++ {
crc64.Checksum(data, table)
}
}
func BenchmarkHashSpeed_Blake2bSimd256(b *testing.B) {
data := make([]byte, dataLen)
letters.Fill(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
blake2b.Sum256(data)
}
}
func BenchmarkHashSpeed_Blake2bSimd512(b *testing.B) {
data := make([]byte, dataLen)
letters.Fill(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
blake2b.Sum512(data)
}
}
func BenchmarkHashSpeed_XXHash(b *testing.B) {
data := make([]byte, dataLen)
letters.Fill(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
xxhash.Sum64(data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment