采用更快的Hash算法

This commit is contained in:
GoEdgeLab
2020-11-21 22:29:57 +08:00
parent 5485a5f663
commit d77749b0cd
12 changed files with 40 additions and 35 deletions

View File

@@ -2,7 +2,7 @@ package grids
import (
"crypto/md5"
"github.com/dchest/siphash"
"github.com/cespare/xxhash"
"strconv"
"testing"
)
@@ -22,7 +22,7 @@ func BenchmarkItem_Size(b *testing.B) {
Key: []byte("123"),
ValueBytes: []byte("Hello, World"),
}
for i := 0; i < b.N; i ++ {
for i := 0; i < b.N; i++ {
_ = item.Size()
}
}
@@ -31,16 +31,16 @@ func TestItem_HashKey(t *testing.T) {
t.Log(HashKey([]byte("2")))
}
func TestItem_siphash(t *testing.T) {
result := siphash.Hash(0, 0, []byte("123456"))
func TestItem_xxHash(t *testing.T) {
result := xxhash.Sum64([]byte("123456"))
t.Log(result)
}
func TestItem_unique(t *testing.T) {
m := map[uint64]bool{}
for i := 0; i < 1000*10000; i ++ {
for i := 0; i < 1000*10000; i++ {
s := "Hello,World,LONG KEY,LONG KEY,LONG KEY,LONG KEY" + strconv.Itoa(i)
result := siphash.Hash(0, 0, []byte(s))
result := xxhash.Sum64([]byte(s))
_, ok := m[result]
if ok {
t.Log("found same", i)
@@ -50,20 +50,20 @@ func TestItem_unique(t *testing.T) {
}
}
t.Log(siphash.Hash(0, 0, []byte("01")))
t.Log(siphash.Hash(0, 0, []byte("10")))
t.Log(xxhash.Sum64([]byte("01")))
t.Log(xxhash.Sum64([]byte("10")))
}
func BenchmarkItem_HashKeyMd5(b *testing.B) {
for i := 0; i < b.N; i ++ {
for i := 0; i < b.N; i++ {
h := md5.New()
h.Write([]byte("HELLO_KEY_" + strconv.Itoa(i)))
_ = h.Sum(nil)
}
}
func BenchmarkItem_siphash(b *testing.B) {
for i := 0; i < b.N; i ++ {
_ = siphash.Hash(0, 0, []byte("HELLO_KEY_"+strconv.Itoa(i)))
func BenchmarkItem_xxHash(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = xxhash.Sum64([]byte("HELLO_KEY_" + strconv.Itoa(i)))
}
}