github.com/dolthub/maphash@v0.1.0/hasher_bench_test.go (about) 1 // Copyright 2022 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:build go1.19 16 // +build go1.19 17 18 package maphash 19 20 import ( 21 gohash "hash/maphash" 22 "math/rand" 23 "testing" 24 ) 25 26 func BenchmarkUint64Hasher(b *testing.B) { 27 h := NewHasher[uint64]() 28 b.ResetTimer() 29 for i := 0; i < b.N; i++ { 30 h.Hash(uint64(i)) 31 } 32 b.ReportAllocs() 33 } 34 35 func BenchmarkCompareStringHasher(b *testing.B) { 36 h := NewHasher[string]() 37 seed := gohash.MakeSeed() 38 const cnt uint64 = 4096 39 const mod uint64 = 4096 - 1 40 data := genStringData(cnt, 16) 41 b.ResetTimer() 42 43 b.Run("string hasher", func(b *testing.B) { 44 for i := 0; i < b.N; i++ { 45 h.Hash(data[uint64(i)&mod]) 46 } 47 b.ReportAllocs() 48 }) 49 b.Run("std string hasher", func(b *testing.B) { 50 for i := 0; i < b.N; i++ { 51 gohash.String(seed, data[uint64(i)&mod]) 52 } 53 b.ReportAllocs() 54 }) 55 } 56 57 func BenchmarkStringPairHasher(b *testing.B) { 58 type pair struct { 59 a, b string 60 } 61 h := NewHasher[pair]() 62 const cnt uint64 = 4096 63 const mod uint64 = 4096 - 1 64 dataA := genStringData(cnt, 16) 65 dataB := genStringData(cnt, 16) 66 b.ResetTimer() 67 for i := 0; i < b.N; i++ { 68 h.Hash(pair{ 69 a: dataA[uint64(i)&mod], 70 b: dataB[uint64(i)&mod], 71 }) 72 } 73 b.ReportAllocs() 74 } 75 76 var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") 77 78 func genStringData(cnt, ln uint64) (data []string) { 79 str := func(n uint64) string { 80 s := make([]rune, n) 81 for i := range s { 82 s[i] = letters[rand.Intn(52)] 83 } 84 return string(s) 85 } 86 data = make([]string, cnt) 87 for i := range data { 88 data[i] = str(ln) 89 } 90 return 91 }