-
-
Save bootjp/7ab7b55d2e592d65b78bce8bc263cb5f to your computer and use it in GitHub Desktop.
Golang hash benchmark
This file contains 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 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