Skip to content

Instantly share code, notes, and snippets.

@maruel
Last active May 16, 2019 18:19
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 maruel/723039dd6a244afe71b1469838c2710c to your computer and use it in GitHub Desktop.
Save maruel/723039dd6a244afe71b1469838c2710c to your computer and use it in GitHub Desktop.
package main
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"
"testing"
sha256simd "github.com/minio/sha256-simd"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/sha3"
)
func inner(b *testing.B, h hash.Hash) {
buf := make([]byte, 256*b.N)
b.ResetTimer()
_, _ = h.Write(buf)
sum := h.Sum(nil)
b.StopTimer()
b.SetBytes(int64(len(buf)))
fmt.Sprintf("%x", sum)
}
func BenchmarkSHA1(b *testing.B) {
inner(b, sha1.New())
}
func BenchmarkSHA2_256(b *testing.B) {
inner(b, sha256.New())
}
func BenchmarkSHA2SIMD_256(b *testing.B) {
inner(b, sha256simd.New())
}
func BenchmarkSHA2_512(b *testing.B) {
inner(b, sha512.New())
}
func BenchmarkSHA3_256(b *testing.B) {
inner(b, sha3.New256())
}
func BenchmarkSHA3_512(b *testing.B) {
inner(b, sha3.New512())
}
func BenchmarkBlake2b_256(b *testing.B) {
h, _ := blake2b.New256(nil)
inner(b, h)
}
func BenchmarkBlake2b_512(b *testing.B) {
h, _ := blake2b.New512(nil)
inner(b, h)
}
// Fails on macPro:
func BenchmarkSHA2AVX512_256(b *testing.B) {
server := sha256simd.NewAvx512Server()
inner(b, sha256simd.NewAvx512(server))
}
@maruel
Copy link
Author

maruel commented May 16, 2019

BenchmarkSHA1-12 5000000 265 ns/op
BenchmarkSHA256-12 3000000 525 ns/op
BenchmarkSHA512-12 5000000 352 ns/op

@maruel
Copy link
Author

maruel commented May 16, 2019

BenchmarkSHA1-12 5000000 264 ns/op
BenchmarkSHA2_256-12 3000000 521 ns/op
BenchmarkSHA2_512-12 5000000 349 ns/op
BenchmarkSHA3_256-12 2000000 629 ns/op
BenchmarkSHA3_512-12 1000000 1175 ns/op
BenchmarkBlake2b_256-12 10000000 269 ns/op
BenchmarkBlake2b_512-12 10000000 207 ns/op

@maruel
Copy link
Author

maruel commented May 16, 2019

BenchmarkSHA1-12                 5000000               265 ns/op        4813942259.92 MB/s
BenchmarkSHA2_256-12             3000000               517 ns/op        1483726629.29 MB/s
BenchmarkSHA2SIMD_256-12         3000000               460 ns/op        1669159659.98 MB/s
BenchmarkSHA2_512-12             5000000               355 ns/op        3605129287.82 MB/s
BenchmarkSHA3_256-12             2000000               632 ns/op        809724346.58 MB/s
BenchmarkSHA3_512-12             1000000              1181 ns/op        216606125.54 MB/s
BenchmarkBlake2b_256-12         10000000               268 ns/op        9542180350.94 MB/s
BenchmarkBlake2b_512-12         10000000               205 ns/op        12454513659.27 MB/s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment