Skip to content

Instantly share code, notes, and snippets.

@costela
Created June 8, 2021 06:56
Show Gist options
  • Save costela/66c878addb7f25277a9440a360ccee69 to your computer and use it in GitHub Desktop.
Save costela/66c878addb7f25277a9440a360ccee69 to your computer and use it in GitHub Desktop.
test xxhash distribution
package main
import (
"bufio"
"fmt"
"math"
"os"
"github.com/cespare/xxhash"
"gonum.org/v1/gonum/floats"
"gonum.org/v1/gonum/stat"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
distribution := make([]float64, 10)
for scanner.Scan() {
line := scanner.Text()
f := hashToFloat(line)
distribution[int(math.Floor(f*10))]++
}
fmt.Println("\nBuckets:\n")
for i := range distribution {
f := float64(i)
fmt.Printf("[%.1f, %.1f] = %.1f\n", f/10, (f+1)/10, distribution[i])
}
stddev := stat.StdDev(distribution, nil)
mean := stat.Mean(distribution, nil)
stderr := stat.StdErr(stddev, floats.Sum(distribution))
fmt.Println("\nTotal:\n")
fmt.Printf("mean: %.2f ± %.2f\n", mean, stderr)
}
func hashToFloat(in string) float64 {
h := xxhash.New()
h.Write([]byte(in))
return float64(h.Sum64()) / math.MaxUint64
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment