Skip to content

Instantly share code, notes, and snippets.

@bootjp
Forked from wizjin/hash_bench_test.go
Created June 25, 2022 08:27
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 bootjp/7ab7b55d2e592d65b78bce8bc263cb5f to your computer and use it in GitHub Desktop.
Save bootjp/7ab7b55d2e592d65b78bce8bc263cb5f to your computer and use it in GitHub Desktop.
Golang hash benchmark
package proto
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash"
"hash/fnv"
"testing"
)
const (
K = 1024
DATALEN = 512 * K
)
func runHash(b *testing.B, h hash.Hash, n int) {
var data = make([]byte, n)
for i := 0; i < n; i++ {
data[i] = byte(i * i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
h.Reset()
h.Sum(data)
}
}
func BenchmarkFNV32(b *testing.B) {
runHash(b, fnv.New32(), DATALEN)
}
func BenchmarkFNV64(b *testing.B) {
runHash(b, fnv.New64(), DATALEN)
}
func BenchmarkFNV32a(b *testing.B) {
runHash(b, fnv.New32a(), DATALEN)
}
func BenchmarkFNV64a(b *testing.B) {
runHash(b, fnv.New64a(), DATALEN)
}
func BenchmarkFNV128(b *testing.B) {
runHash(b, fnv.New128(), DATALEN)
}
func BenchmarkFNV128a(b *testing.B) {
runHash(b, fnv.New128a(), DATALEN)
}
func BenchmarkMD5(b *testing.B) {
runHash(b, md5.New(), DATALEN)
}
func BenchmarkSHA1(b *testing.B) {
runHash(b, sha1.New(), DATALEN)
}
func BenchmarkSHA224(b *testing.B) {
runHash(b, sha256.New224(), DATALEN)
}
func BenchmarkSHA256(b *testing.B) {
runHash(b, sha256.New(), DATALEN)
}
func BenchmarkSHA512(b *testing.B) {
runHash(b, sha512.New(), DATALEN)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment