Skip to content

Instantly share code, notes, and snippets.

@ja7ad
Last active November 30, 2024 09:11
Show Gist options
  • Save ja7ad/b8c77a6cec8751b6ec9aeb0a14cbedc6 to your computer and use it in GitHub Desktop.
Save ja7ad/b8c77a6cec8751b6ec9aeb0a14cbedc6 to your computer and use it in GitHub Desktop.
func Hash256(data []byte) []byte {
h := blake2b.Sum256(data)
return h[:]
}
func CalcHashWithXXHash64(data []byte) []byte {
xh := xxhash.New64()
_, _ = xh.Write(data)
b := xh.Sum(nil)
return b
}
func CalcHashWithXXHash32(data []byte) []byte {
xh := xxhash.New32()
_, _ = xh.Write(data)
b := xh.Sum(nil)
return b
}
func BenchmarkCompareWithXXHash(b *testing.B) {
data := []byte("pactus")
b.Run("XXHash 64 Algorithm", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = hash.CalcHashWithXXHash64(data)
}
})
b.Run("XXHash 32 Algorithm", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = hash.CalcHashWithXXHash32(data)
}
})
b.Run("Blake2b Hash Algorithm", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = hash.Hash256(data)
}
})
}
/*
Results:
cpu: AMD Ryzen 7 5700U with Radeon Graphics
BenchmarkCompareWithXXHash
BenchmarkCompareWithXXHash/XXHash_64_Algorithm
BenchmarkCompareWithXXHash/XXHash_64_Algorithm-16 28792616 40.06 ns/op 8 B/op 1 allocs/op
BenchmarkCompareWithXXHash/XXHash_32_Algorithm
BenchmarkCompareWithXXHash/XXHash_32_Algorithm-16 29995200 37.13 ns/op 8 B/op 1 allocs/op
BenchmarkCompareWithXXHash/Blake2b_Hash_Algorithm
BenchmarkCompareWithXXHash/Blake2b_Hash_Algorithm-16 8504932 139.6 ns/op 0 B/op 0 allocs/op
PASS
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment