github.com/gogf/gf/v2@v2.7.4/container/gset/gset_z_bench_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // go test *.go -bench=".*" -benchmem 8 9 package gset_test 10 11 import ( 12 "strconv" 13 "testing" 14 15 "github.com/gogf/gf/v2/container/gset" 16 ) 17 18 var intSet = gset.NewIntSet(true) 19 20 var anySet = gset.NewSet(true) 21 22 var strSet = gset.NewStrSet(true) 23 24 var intSetUnsafe = gset.NewIntSet() 25 26 var anySetUnsafe = gset.NewSet() 27 28 var strSetUnsafe = gset.NewStrSet() 29 30 func Benchmark_IntSet_Add(b *testing.B) { 31 b.RunParallel(func(pb *testing.PB) { 32 i := 0 33 for pb.Next() { 34 intSet.Add(i) 35 i++ 36 } 37 }) 38 } 39 40 func Benchmark_IntSet_Contains(b *testing.B) { 41 b.RunParallel(func(pb *testing.PB) { 42 i := 0 43 for pb.Next() { 44 intSet.Contains(i) 45 i++ 46 } 47 }) 48 } 49 50 func Benchmark_IntSet_Remove(b *testing.B) { 51 b.RunParallel(func(pb *testing.PB) { 52 i := 0 53 for pb.Next() { 54 intSet.Remove(i) 55 i++ 56 } 57 }) 58 } 59 60 func Benchmark_AnySet_Add(b *testing.B) { 61 b.RunParallel(func(pb *testing.PB) { 62 i := 0 63 for pb.Next() { 64 anySet.Add(i) 65 i++ 66 } 67 }) 68 } 69 70 func Benchmark_AnySet_Contains(b *testing.B) { 71 b.RunParallel(func(pb *testing.PB) { 72 i := 0 73 for pb.Next() { 74 anySet.Contains(i) 75 i++ 76 } 77 }) 78 } 79 80 func Benchmark_AnySet_Remove(b *testing.B) { 81 b.RunParallel(func(pb *testing.PB) { 82 i := 0 83 for pb.Next() { 84 anySet.Remove(i) 85 i++ 86 } 87 }) 88 } 89 90 // Note that there's additional performance cost for string conversion. 91 func Benchmark_StrSet_Add(b *testing.B) { 92 b.RunParallel(func(pb *testing.PB) { 93 i := 0 94 for pb.Next() { 95 strSet.Add(strconv.Itoa(i)) 96 i++ 97 } 98 }) 99 } 100 101 // Note that there's additional performance cost for string conversion. 102 func Benchmark_StrSet_Contains(b *testing.B) { 103 b.RunParallel(func(pb *testing.PB) { 104 i := 0 105 for pb.Next() { 106 strSet.Contains(strconv.Itoa(i)) 107 i++ 108 } 109 }) 110 } 111 112 // Note that there's additional performance cost for string conversion. 113 func Benchmark_StrSet_Remove(b *testing.B) { 114 b.RunParallel(func(pb *testing.PB) { 115 i := 0 116 for pb.Next() { 117 strSet.Remove(strconv.Itoa(i)) 118 i++ 119 } 120 }) 121 } 122 123 func Benchmark_Unsafe_IntSet_Add(b *testing.B) { 124 for i := 0; i < b.N; i++ { 125 intSetUnsafe.Add(i) 126 } 127 } 128 129 func Benchmark_Unsafe_IntSet_Contains(b *testing.B) { 130 for i := 0; i < b.N; i++ { 131 intSetUnsafe.Contains(i) 132 } 133 } 134 135 func Benchmark_Unsafe_IntSet_Remove(b *testing.B) { 136 for i := 0; i < b.N; i++ { 137 intSetUnsafe.Remove(i) 138 } 139 } 140 141 func Benchmark_Unsafe_AnySet_Add(b *testing.B) { 142 for i := 0; i < b.N; i++ { 143 anySetUnsafe.Add(i) 144 } 145 } 146 147 func Benchmark_Unsafe_AnySet_Contains(b *testing.B) { 148 for i := 0; i < b.N; i++ { 149 anySetUnsafe.Contains(i) 150 } 151 } 152 153 func Benchmark_Unsafe_AnySet_Remove(b *testing.B) { 154 for i := 0; i < b.N; i++ { 155 anySetUnsafe.Remove(i) 156 } 157 } 158 159 // Note that there's additional performance cost for string conversion. 160 func Benchmark_Unsafe_StrSet_Add(b *testing.B) { 161 for i := 0; i < b.N; i++ { 162 strSetUnsafe.Add(strconv.Itoa(i)) 163 } 164 } 165 166 // Note that there's additional performance cost for string conversion. 167 func Benchmark_Unsafe_StrSet_Contains(b *testing.B) { 168 for i := 0; i < b.N; i++ { 169 strSetUnsafe.Contains(strconv.Itoa(i)) 170 } 171 } 172 173 // Note that there's additional performance cost for string conversion. 174 func Benchmark_Unsafe_StrSet_Remove(b *testing.B) { 175 for i := 0; i < b.N; i++ { 176 strSetUnsafe.Remove(strconv.Itoa(i)) 177 } 178 }