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