Hash Speed in Golang
When choosing a hash function in Go, performance is often a crucial factor. This article compares the speed of various hash functions available in Go’s standard library: MD5, SHA1, SHA256, SHA512, FNV-1 and FNV-1a (32/64/128), CRC32, CRC64, and maphash.
Benchmark Setup
All benchmarks were run on an Apple M3 Pro processor using Go’s built-in testing framework. The test data consists of a 100-character string: “The quick brown fox jumps over the lazy dog. This is a sample text for benchmarking various hash functions in Go.”
var testData = []byte("The quick brown fox jumps over the lazy dog. This is a sample text for benchmarking various hash functions in Go.")
Results
Hash Function | Hash Size | Time per Operation | Memory per Operation | Allocations per Operation |
---|---|---|---|---|
CRC32 | 32-bit | 26.02 ns/op | 8 B/op | 1 allocs/op |
CRC64 | 64-bit | 78.24 ns/op | 8 B/op | 1 allocs/op |
FNV-1 128 | 128-bit | 487.5 ns/op | 24 B/op | 2 allocs/op |
FNV-1 32 | 32-bit | 131.9 ns/op | 8 B/op | 1 allocs/op |
FNV-1 64 | 64-bit | 135.7 ns/op | 8 B/op | 1 allocs/op |
FNV-1a 128 | 128-bit | 533.1 ns/op | 24 B/op | 2 allocs/op |
FNV-1a 32 | 32-bit | 132.1 ns/op | 8 B/op | 1 allocs/op |
FNV-1a 64 | 64-bit | 138.4 ns/op | 8 B/op | 1 allocs/op |
maphash | 64-bit | 16.14 ns/op | 0 B/op | 0 allocs/op |
MD5 | 128-bit | 253.6 ns/op | 16 B/op | 1 allocs/op |
SHA1 | 160-bit | 98.89 ns/op | 24 B/op | 1 allocs/op |
SHA256 | 256-bit | 93.53 ns/op | 32 B/op | 1 allocs/op |
SHA512 | 512-bit | 277.4 ns/op | 64 B/op | 1 allocs/op |
Analysis
Cryptographic Hashes
Among cryptographic hash functions, SHA256 (93.53 ns/op) surprisingly outperforms SHA1 (98.89 ns/op) on Apple Silicon, likely due to hardware acceleration. MD5 (253.6 ns/op) and SHA512 (277.4 ns/op) are the slowest cryptographic options tested.
FNV Hash Family
The FNV hash functions show moderate performance:
- FNV-1 and FNV-1a variants perform similarly within each bit size
- 32-bit variants: FNV-1 (131.9 ns/op) vs FNV-1a (132.1 ns/op)
- 64-bit variants: FNV-1 (135.7 ns/op) vs FNV-1a (138.4 ns/op)
- 128-bit variants are significantly slower: FNV-1 (487.5 ns/op) vs FNV-1a (533.1 ns/op)
- All 128-bit variants require 2 allocations instead of 1
FNV-1 vs FNV-1a difference: The main difference between FNV-1 and FNV-1a is the order of operations. FNV-1 multiplies first then XORs, while FNV-1a XORs first then multiplies. FNV-1a generally provides better avalanche characteristics and distribution for short inputs, making it slightly more suitable for hash tables, though both produce high-quality hashes.