gf框架提供了几个非常实用的并发安全容器,其中gmap就是项目开发中最常用的一个。
gmap具体的方法请参考godoc:https://godoc.org/github.com/johng-cn/gf/g/container/gmap
gmap内部有多个类型结构体定义,包括:IntBoolMap、IntIntMap、IntInterfaceMap、IntStringMap、InterfaceInterfaceMap、StringBoolMap、StringIntMap、StringInterfaceMap、StringStringMap、UintInterfaceMap。
从执行效率上考虑,基于不同的需求场景,选择合适的类型结构体,其执行效率是不一样的,以下使用基准测试来对比各个类型的写入性能(测试代码):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
john@johnstation:~/Workspace/Go/GOPATH/src/gitee.com/johng/gf/g/container/gmap$ go test gmap_test.go -bench=".*" goos: linux goarch: amd64 BenchmarkIntBoolMap_Set-8 10000000 171 ns/op BenchmarkIntIntMap_Set-8 10000000 181 ns/op BenchmarkIntInterfaceMap_Set-8 10000000 227 ns/op BenchmarkIntStringMap_Set-8 10000000 271 ns/op BenchmarkInterfaceInterfaceMap_Set-8 5000000 331 ns/op BenchmarkStringBoolMap_Set-8 5000000 271 ns/op BenchmarkStringIntMap_Set-8 5000000 300 ns/op BenchmarkStringInterfaceMap_Set-8 5000000 363 ns/op BenchmarkStringStringMap_Set-8 5000000 394 ns/op BenchmarkUintInterfaceMap_Set-8 10000000 275 ns/op PASS ok command-line-arguments 37.024s |