Skip to content

Instantly share code, notes, and snippets.

@phemmer
Created May 12, 2015 16:31
Show Gist options
  • Select an option

  • Save phemmer/24ab81be35b79b1d4f1d to your computer and use it in GitHub Desktop.

Select an option

Save phemmer/24ab81be35b79b1d4f1d to your computer and use it in GitHub Desktop.
go hash benchmark
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash/crc32"
"testing"
)
var pathIn = []byte("/foo/12/345")
var pathOut string
func BenchmarkCRC32(b *testing.B) {
for i := 0; i < b.N; i++ {
pathOut = string(pathIn) + "-" + string(crc32.ChecksumIEEE(pathIn))
}
}
func BenchmarkMD5(b *testing.B) {
for i := 0; i < b.N; i++ {
pathOut = string(pathIn) + "-" + fmt.Sprintf("%x", md5.Sum(pathIn))
}
}
func BenchmarkSHA1(b *testing.B) {
for i := 0; i < b.N; i++ {
pathOut = string(pathIn) + "-" + fmt.Sprintf("%x", sha1.Sum(pathIn))
}
}
func BenchmarkSHA224(b *testing.B) {
for i := 0; i < b.N; i++ {
pathOut = string(pathIn) + "-" + fmt.Sprintf("%x", sha256.Sum224(pathIn))
}
}
func BenchmarkSHA256(b *testing.B) {
for i := 0; i < b.N; i++ {
pathOut = string(pathIn) + "-" + fmt.Sprintf("%x", sha256.Sum256(pathIn))
}
}
# go test -v -bench=. ./
testing: warning: no tests to run
PASS
BenchmarkCRC32 5000000 281 ns/op
BenchmarkMD5 1000000 2021 ns/op
BenchmarkSHA1 1000000 2383 ns/op
BenchmarkSHA224 500000 3264 ns/op
BenchmarkSHA256 500000 3292 ns/op
ok tmp/n61 10.466s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment