Created
November 23, 2021 20:54
-
-
Save gammazero/b76e03e3adc394cadcee574157f2c52d to your computer and use it in GitHub Desktop.
Benchmark different hashes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/md5" | |
"crypto/sha1" | |
"crypto/sha256" | |
"crypto/sha512" | |
"hash" | |
"hash/fnv" | |
"testing" | |
simdblake2b "github.com/minio/blake2b-simd" | |
simdsha256 "github.com/minio/sha256-simd" | |
"golang.org/x/crypto/blake2b" | |
"golang.org/x/crypto/sha3" | |
"lukechampine.com/blake3" | |
) | |
const ( | |
K = 1024 | |
DATALEN = 1 * 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 BenchmarkFNV128(b *testing.B) { | |
runHash(b, fnv.New128(), 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) | |
} | |
func BenchmarkBlake2b256(b *testing.B) { | |
h, err := blake2b.New256(nil) | |
if err != nil { | |
panic(err) | |
} | |
runHash(b, h, DATALEN) | |
} | |
func BenchmarkSHA3_224(b *testing.B) { | |
runHash(b, sha3.New224(), DATALEN) | |
} | |
func BenchmarkSHA3_256(b *testing.B) { | |
runHash(b, sha3.New256(), DATALEN) | |
} | |
func BenchmarkSimdSHA256(b *testing.B) { | |
runHash(b, simdsha256.New(), DATALEN) | |
} | |
func BenchmarkSimdBlake2b256(b *testing.B) { | |
runHash(b, simdblake2b.New256(), DATALEN) | |
} | |
func BenchmarkBlake3_256(b *testing.B) { | |
runHash(b, blake3.New(256, nil), DATALEN) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz | |
BenchmarkFNV32-8 6902038 175.7 ns/op | |
BenchmarkFNV64-8 6833394 178.3 ns/op | |
BenchmarkFNV128-8 6837572 177.1 ns/op | |
BenchmarkMD5-8 4465626 267.7 ns/op | |
BenchmarkSHA1-8 4065307 293.5 ns/op | |
BenchmarkSHA224-8 3386400 354.5 ns/op | |
BenchmarkSHA256-8 3388011 355.0 ns/op | |
BenchmarkSHA512-8 2856084 418.3 ns/op | |
BenchmarkBlake2b256-8 3856017 310.7 ns/op | |
BenchmarkSHA3_224-8 1547455 851.5 ns/op | |
BenchmarkSHA3_256-8 1241260 987.8 ns/op | |
BenchmarkSimdSHA256-8 2663432 450.7 ns/op | |
BenchmarkSimdBlake2b256-8 2463177 487.8 ns/op | |
BenchmarkBlake3_256-8 1828723 654.2 ns/op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment