Last active
June 3, 2020 02:37
-
-
Save xeoncross/bcc5f50fcd01f4e33cc01933f41aadbe to your computer and use it in GitHub Desktop.
Tests to see how 64bit hash functions perform in Go.
This file contains 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/sha256" | |
"encoding/hex" | |
"fmt" | |
"hash/fnv" | |
"testing" | |
"time" | |
"github.com/cespare/xxhash/v2" | |
"golang.org/x/exp/rand" | |
) | |
func TestEachHash(t *testing.T) { | |
hashes := make(map[string]struct{}) | |
max := 10000000 | |
var b [40]byte | |
for i := 0; i < max; i++ { | |
if i%1000000 == 0 { | |
time.Sleep(time.Millisecond) | |
} | |
_, err := rand.Read(b[:]) | |
if err != nil { | |
t.Error(err) | |
t.Fail() | |
} | |
// result := sha256hash(b[:]) | |
// result := fnv1ahash(b[:]) | |
result := xxhashhelper(b[:]) | |
// Sanity check | |
if i < 5 { | |
fmt.Println(i, result) | |
} | |
if _, ok := hashes[result]; ok { | |
t.Logf("%s == %v", result, b) | |
t.Errorf("Found collision after %d searches", i) | |
} | |
hashes[result] = struct{}{} | |
} | |
fmt.Println("no collisions found", max) | |
} | |
func TestXXHash(t *testing.T) { | |
hashes := make(map[uint64]struct{}) | |
max := 50000000 | |
var b [40]byte | |
for i := 0; i < max; i++ { | |
if i%10000000 == 0 { | |
t.Logf("iteration: %d\n", i) | |
} | |
_, err := rand.Read(b[:]) | |
if err != nil { | |
t.Error(err) | |
t.Fail() | |
} | |
result := xxhash.Sum64(b[:]) | |
// Sanity check | |
if i < 5 { | |
fmt.Println(i, result) | |
} | |
if _, ok := hashes[result]; ok { | |
t.Logf("%d == %v", result, b) | |
t.Errorf("Found collision after %d searches", i) | |
} | |
hashes[result] = struct{}{} | |
} | |
fmt.Println("no collisions found", max) | |
} | |
func sha256hash(b []byte) string { | |
h := sha256.New() | |
h.Write(b) | |
var r [8]byte | |
copy(r[:], h.Sum(nil)) | |
return hex.EncodeToString(r[:]) | |
} | |
func fnv1ahash(b []byte) string { | |
h := fnv.New64a() | |
h.Write(b) | |
var r [8]byte | |
copy(r[:], h.Sum(nil)) | |
return hex.EncodeToString(r[:]) | |
} | |
func xxhashhelper(b []byte) string { | |
h := xxhash.New() | |
h.Write(b) | |
var r [8]byte | |
copy(r[:], h.Sum(nil)) | |
return hex.EncodeToString(r[:]) | |
// return fmt.Sprintf("%d", xxhash.Sum64(b)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment