Introduction

Common classic hash functions implemented in Go language are provided, offering hash functions for uint32 and uint64 types.

Usage:

  1. import "github.com/gogf/gf/v2/encoding/ghash"

API Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/encoding/ghash

Benchmark

  1. goos: darwin
  2. goarch: amd64
  3. pkg: github.com/gogf/gf/v2/encoding/ghash
  4. cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  5. Benchmark_BKDR
  6. Benchmark_BKDR-12 39315165 26.88 ns/op
  7. Benchmark_BKDR64
  8. Benchmark_BKDR64-12 62891215 22.61 ns/op
  9. Benchmark_SDBM
  10. Benchmark_SDBM-12 49689925 25.40 ns/op
  11. Benchmark_SDBM64
  12. Benchmark_SDBM64-12 48860472 24.38 ns/op
  13. Benchmark_RS
  14. Benchmark_RS-12 39463418 25.52 ns/op
  15. Benchmark_RS64
  16. Benchmark_RS64-12 53318370 19.45 ns/op
  17. Benchmark_JS
  18. Benchmark_JS-12 53751033 23.20 ns/op
  19. Benchmark_JS64
  20. Benchmark_JS64-12 62440287 19.25 ns/op
  21. Benchmark_PJW
  22. Benchmark_PJW-12 42439626 27.85 ns/op
  23. Benchmark_PJW64
  24. Benchmark_PJW64-12 37491696 33.28 ns/op
  25. Benchmark_ELF
  26. Benchmark_ELF-12 38034584 31.74 ns/op
  27. Benchmark_ELF64
  28. Benchmark_ELF64-12 44047201 27.58 ns/op
  29. Benchmark_DJB
  30. Benchmark_DJB-12 46994352 22.60 ns/op
  31. Benchmark_DJB64
  32. Benchmark_DJB64-12 61980186 19.19 ns/op
  33. Benchmark_AP
  34. Benchmark_AP-12 29544234 40.58 ns/op
  35. Benchmark_AP64
  36. Benchmark_AP64-12 28123783 42.48 ns/op

Repeated Tests

Test results are influenced by the test content’s associativity and randomness. Here, I perform simple repeatability tests by traversing the range of uint64 values, which is not rigorous in itself and is meant for fun reference only.

  1. package main
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "math"
  6. "github.com/gogf/gf/v2/encoding/ghash"
  7. )
  8. func main() {
  9. var (
  10. m = make(map[uint64]struct{})
  11. b = make([]byte, 8)
  12. ok bool
  13. hash uint64
  14. )
  15. for i := uint64(0); i < math.MaxUint64; i++ {
  16. binary.LittleEndian.PutUint64(b, i)
  17. hash = ghash.PJW64(b)
  18. if _, ok = m[hash]; ok {
  19. fmt.Println("repeated found:", i)
  20. break
  21. }
  22. m[hash] = struct{}{}
  23. }
  24. }

The test results are as follows:

Hash FunctionRepeat LocationRemarks
AP648388640
BKDR6433536
DJB648448
ELF644096
JS64734
PJW642
RS64-32G Memory OOM
SDBM64-32G Memory OOM