github.com/gogf/gf/v2@v2.7.4/container/gmap/gmap_z_bench_safe_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 gm file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // go test *.go -bench=".*" -benchmem 8 9 package gmap_test 10 11 import ( 12 "strconv" 13 "testing" 14 15 "github.com/gogf/gf/v2/container/gmap" 16 ) 17 18 var anyAnyMap = gmap.NewAnyAnyMap(true) 19 20 var intIntMap = gmap.NewIntIntMap(true) 21 22 var intAnyMap = gmap.NewIntAnyMap(true) 23 24 var intStrMap = gmap.NewIntStrMap(true) 25 26 var strIntMap = gmap.NewStrIntMap(true) 27 28 var strAnyMap = gmap.NewStrAnyMap(true) 29 30 var strStrMap = gmap.NewStrStrMap(true) 31 32 func Benchmark_IntIntMap_Set(b *testing.B) { 33 b.RunParallel(func(pb *testing.PB) { 34 i := 0 35 for pb.Next() { 36 intIntMap.Set(i, i) 37 i++ 38 } 39 }) 40 } 41 42 func Benchmark_IntAnyMap_Set(b *testing.B) { 43 b.RunParallel(func(pb *testing.PB) { 44 i := 0 45 for pb.Next() { 46 intAnyMap.Set(i, i) 47 i++ 48 } 49 }) 50 } 51 52 func Benchmark_IntStrMap_Set(b *testing.B) { 53 b.RunParallel(func(pb *testing.PB) { 54 i := 0 55 for pb.Next() { 56 intStrMap.Set(i, "123456789") 57 i++ 58 } 59 }) 60 } 61 62 func Benchmark_AnyAnyMap_Set(b *testing.B) { 63 b.RunParallel(func(pb *testing.PB) { 64 i := 0 65 for pb.Next() { 66 anyAnyMap.Set(i, i) 67 i++ 68 } 69 }) 70 } 71 72 // Note that there's additional performance cost for string conversion. 73 func Benchmark_StrIntMap_Set(b *testing.B) { 74 b.RunParallel(func(pb *testing.PB) { 75 i := 0 76 for pb.Next() { 77 strIntMap.Set(strconv.Itoa(i), i) 78 i++ 79 } 80 }) 81 } 82 83 // Note that there's additional performance cost for string conversion. 84 func Benchmark_StrAnyMap_Set(b *testing.B) { 85 b.RunParallel(func(pb *testing.PB) { 86 i := 0 87 for pb.Next() { 88 strAnyMap.Set(strconv.Itoa(i), i) 89 i++ 90 } 91 }) 92 } 93 94 // Note that there's additional performance cost for string conversion. 95 func Benchmark_StrStrMap_Set(b *testing.B) { 96 b.RunParallel(func(pb *testing.PB) { 97 i := 0 98 for pb.Next() { 99 strStrMap.Set(strconv.Itoa(i), "123456789") 100 i++ 101 } 102 }) 103 } 104 105 func Benchmark_IntIntMap_Get(b *testing.B) { 106 b.RunParallel(func(pb *testing.PB) { 107 i := 0 108 for pb.Next() { 109 intIntMap.Get(i) 110 i++ 111 } 112 }) 113 } 114 115 func Benchmark_IntAnyMap_Get(b *testing.B) { 116 b.RunParallel(func(pb *testing.PB) { 117 i := 0 118 for pb.Next() { 119 intAnyMap.Get(i) 120 i++ 121 } 122 }) 123 } 124 125 func Benchmark_IntStrMap_Get(b *testing.B) { 126 b.RunParallel(func(pb *testing.PB) { 127 i := 0 128 for pb.Next() { 129 intStrMap.Get(i) 130 i++ 131 } 132 }) 133 } 134 135 func Benchmark_AnyAnyMap_Get(b *testing.B) { 136 b.RunParallel(func(pb *testing.PB) { 137 i := 0 138 for pb.Next() { 139 anyAnyMap.Get(i) 140 i++ 141 } 142 }) 143 } 144 145 // Note that there's additional performance cost for string conversion. 146 func Benchmark_StrIntMap_Get(b *testing.B) { 147 b.RunParallel(func(pb *testing.PB) { 148 i := 0 149 for pb.Next() { 150 strIntMap.Get(strconv.Itoa(i)) 151 i++ 152 } 153 }) 154 } 155 156 // Note that there's additional performance cost for string conversion. 157 func Benchmark_StrAnyMap_Get(b *testing.B) { 158 b.RunParallel(func(pb *testing.PB) { 159 i := 0 160 for pb.Next() { 161 strAnyMap.Get(strconv.Itoa(i)) 162 i++ 163 } 164 }) 165 } 166 167 // Note that there's additional performance cost for string conversion. 168 func Benchmark_StrStrMap_Get(b *testing.B) { 169 b.RunParallel(func(pb *testing.PB) { 170 i := 0 171 for pb.Next() { 172 strStrMap.Get(strconv.Itoa(i)) 173 i++ 174 } 175 }) 176 }