Created
May 12, 2015 16:31
-
-
Save phemmer/24ab81be35b79b1d4f1d to your computer and use it in GitHub Desktop.
go hash benchmark
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" | |
| "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)) | |
| } | |
| } |
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
| # 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