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