github.com/songzhibin97/gkit@v1.2.13/structure/hashset/hashset_bench_test.go (about) 1 // Copyright 2021 ByteDance 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 package hashset 16 17 import ( 18 "math/rand" 19 "testing" 20 ) 21 22 type int64SetBool map[int64]bool 23 24 func newInt64Bool() *int64SetBool { 25 return &int64SetBool{} 26 } 27 28 func (s *int64SetBool) Add(value int64) bool { 29 (*s)[value] = true 30 return true 31 } 32 33 func (s *int64SetBool) Contains(value int64) bool { 34 if _, ok := (*s)[value]; ok { 35 return true 36 } 37 return false 38 } 39 40 func (s *int64SetBool) Remove(value int64) bool { 41 delete(*s, value) 42 return true 43 } 44 45 func (s *int64SetBool) Range(f func(value int64) bool) { 46 for k := range *s { 47 if !f(k) { 48 break 49 } 50 } 51 } 52 53 func (s *int64SetBool) Len() int { 54 return len(*s) 55 } 56 57 type int64SetAdd map[int64]struct{} 58 59 func newInt64Add() *int64SetAdd { 60 return &int64SetAdd{} 61 } 62 63 func (s *int64SetAdd) Add(value int64) bool { 64 if s.Contains(value) { 65 return true 66 } 67 (*s)[value] = struct{}{} 68 return true 69 } 70 71 func (s *int64SetAdd) Contains(value int64) bool { 72 if _, ok := (*s)[value]; ok { 73 return true 74 } 75 return false 76 } 77 78 func (s *int64SetAdd) Remove(value int64) bool { 79 if s.Contains(value) { 80 delete(*s, value) 81 return true 82 } 83 return false 84 } 85 86 func (s *int64SetAdd) Range(f func(value int64) bool) { 87 for k := range *s { 88 if !f(k) { 89 break 90 } 91 } 92 } 93 94 func (s *int64SetAdd) Len() int { 95 return len(*s) 96 } 97 98 const capacity = 10000000 99 100 var randomList [capacity]int64 101 102 func init() { 103 for i := 0; i < capacity; i++ { 104 randomList[i] = int64(rand.Int63()) 105 } 106 } 107 108 func BenchmarkValueAsBool(b *testing.B) { 109 b.ResetTimer() 110 l := newInt64Bool() 111 for n := 0; n < b.N; n++ { 112 l.Add(randomList[n%capacity]) 113 } 114 } 115 116 func BenchmarkValueAsEmptyStruct(b *testing.B) { 117 b.ResetTimer() 118 l := NewInt64() 119 for n := 0; n < b.N; n++ { 120 l.Add(randomList[n%capacity]) 121 } 122 } 123 124 func BenchmarkAddAfterContains(b *testing.B) { 125 b.ResetTimer() 126 l := newInt64Add() 127 for n := 0; n < b.N; n++ { 128 l.Add(randomList[n%capacity]) 129 } 130 } 131 132 func BenchmarkAddWithoutContains(b *testing.B) { 133 b.ResetTimer() 134 l := NewInt64() 135 for n := 0; n < b.N; n++ { 136 l.Add(randomList[n%capacity]) 137 } 138 } 139 140 func BenchmarkRemoveAfterContains_Missing(b *testing.B) { 141 l := newInt64Add() 142 for n := 0; n < b.N; n++ { 143 l.Add(randomList[n%capacity]) 144 } 145 b.ResetTimer() 146 147 for n := 0; n < b.N; n++ { 148 l.Remove(int64(rand.Int63())) 149 } 150 } 151 152 func BenchmarkRemoveWithoutContains_Missing(b *testing.B) { 153 154 l := NewInt64() 155 for n := 0; n < b.N; n++ { 156 l.Add(randomList[n%capacity]) 157 } 158 b.ResetTimer() 159 160 for n := 0; n < b.N; n++ { 161 l.Remove(int64(rand.Int63())) 162 } 163 } 164 165 func BenchmarkRemoveAfterContains_Hitting(b *testing.B) { 166 l := newInt64Add() 167 for n := 0; n < b.N; n++ { 168 l.Add(randomList[n%capacity]) 169 } 170 171 b.ResetTimer() 172 for n := 0; n < b.N; n++ { 173 l.Remove(randomList[n%capacity]) 174 } 175 } 176 177 func BenchmarkRemoveWithoutContains_Hitting(b *testing.B) { 178 l := NewInt64() 179 for n := 0; n < b.N; n++ { 180 l.Add(randomList[n%capacity]) 181 } 182 183 b.ResetTimer() 184 for n := 0; n < b.N; n++ { 185 l.Remove(randomList[n%capacity]) 186 } 187 }