Skip to content

Instantly share code, notes, and snippets.

@chowey
Created July 25, 2014 05:39
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 chowey/24a1a23c9f6213fcc65d to your computer and use it in GitHub Desktop.
Save chowey/24a1a23c9f6213fcc65d to your computer and use it in GitHub Desktop.
Benchmark comparison of cryptographic hashes in Go
package main
import (
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"testing"
)
var data []byte
func init() {
// Create a big blob of random data
data = make([]byte, 1024*1024)
rand.Read(data)
}
func BenchmarkMD5(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = md5.Sum(data)
}
}
func BenchmarkSHA1(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sha1.Sum(data)
}
}
func BenchmarkSHA224(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sha256.Sum224(data)
}
}
func BenchmarkSHA256(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sha256.Sum256(data)
}
}
func BenchmarkSHA384(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sha512.Sum384(data)
}
}
func BenchmarkSHA512(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sha512.Sum512(data)
}
}
@chowey
Copy link
Author

chowey commented Jul 25, 2014

On my Windows box:

BenchmarkMD5        1000           2506318 ns/op
BenchmarkSHA1       1000           2993381 ns/op
BenchmarkSHA224      100          10041285 ns/op
BenchmarkSHA256      100          10031281 ns/op
BenchmarkSHA384       50          35564550 ns/op
BenchmarkSHA512       50          35324482 ns/op

@chowey
Copy link
Author

chowey commented Jul 25, 2014

On my Mac box:

BenchmarkMD5        1000       2012278 ns/op
BenchmarkSHA1       1000       2642285 ns/op
BenchmarkSHA224      500       7032779 ns/op
BenchmarkSHA256      500       6978278 ns/op
BenchmarkSHA384      500       4506975 ns/op
BenchmarkSHA512      500       4517548 ns/op

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