Skip to content

Instantly share code, notes, and snippets.

@mhr3
Created April 18, 2020 11:48
Show Gist options
  • Select an option

  • Save mhr3/3cb095e02b759dbb9b605b5ada152bc1 to your computer and use it in GitHub Desktop.

Select an option

Save mhr3/3cb095e02b759dbb9b605b5ada152bc1 to your computer and use it in GitHub Desktop.
package hash_test
import (
"fmt"
"strings"
"testing"
"unsafe"
"github.com/segmentio/fasthash/fnv1a"
)
func BenchmarkCopies(b *testing.B) {
testCases := []string{
strings.Repeat("a", 8),
strings.Repeat("a", 64),
strings.Repeat("a", 128),
}
for _, s := range testCases {
byteArr := []byte(s)
b.Run(fmt.Sprintf("string%d", len(s)), func(b *testing.B) {
for i := 0; i < b.N; i++ {
fnv1a.HashString64(s)
}
})
b.Run(fmt.Sprintf("[%d]byte", len(s)), func(b *testing.B) {
for i := 0; i < b.N; i++ {
fnv1a.HashString64(string(byteArr))
}
})
b.Run(fmt.Sprintf("unsafe%d", len(s)), func(b *testing.B) {
for i := 0; i < b.N; i++ {
fnv1a.HashString64(*(*string)(unsafe.Pointer(&byteArr)))
}
})
}
}
/*
goos: linux
goarch: amd64
pkg: axicode.axiom.co/watchmakers/eventdb/common
BenchmarkCopies/string8-8 100000000 11.4 ns/op 0 B/op 0 allocs/op
BenchmarkCopies/[8]byte-8 64843872 18.3 ns/op 0 B/op 0 allocs/op
BenchmarkCopies/unsafe8-8 100000000 10.9 ns/op 0 B/op 0 allocs/op
BenchmarkCopies/string64-8 11543194 101 ns/op 0 B/op 0 allocs/op
BenchmarkCopies/[64]byte-8 4585114 373 ns/op 64 B/op 1 allocs/op
BenchmarkCopies/unsafe64-8 11537672 98.9 ns/op 0 B/op 0 allocs/op
BenchmarkCopies/string128-8 4799910 234 ns/op 0 B/op 0 allocs/op
BenchmarkCopies/[128]byte-8 2291605 573 ns/op 128 B/op 1 allocs/op
BenchmarkCopies/unsafe128-8 5121356 243 ns/op 0 B/op 0 allocs/op
PASS
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment