With map[int32]*int32, GC took 2.831259536s
With map[int32]int32, GC took 541.091193ms
With map shards ([]map[int32]int32), GC took 325.312192ms
With a plain slice ([]main.t), GC took 69.359µs
Higher parallelism does help, to some extent:
for t in 1 2 3 4; do GOMAXPROCS=4 go run maps.go $t; done
With map[int32]*int32, GC took 2.347938525s
With map[int32]int32, GC took 362.263322ms
With map shards ([]map[int32]int32), GC took 89.884292ms
With a plain slice ([]main.t), GC took 312.583µs
其实这取决于这个map是什么样的map,以及你的go的版本,在go 1.4版本存在map gc的bug。具体细节可以参考我的这篇文章。
对于你的问题,在四种情况gc时间情况:
map[int32]*int32 即value为指针:
map[int32]int32 即value为对象引用:
将map分为多个小map(高性能map常用做法)
而使用slice代替map处理的话,即[]t
如果将
GOMAXPROCS
设置为4则结果如下:runtime.GOMAXPROCS(4)
map[int32]*int32 即value为指针:
map[int32]int32 即value为对象引用:
将map分为多个小map(高性能map常用做法)
而使用slice代替map处理的话,即[]t
可以得出结论,
gc
时间受到map存储的类型和CPU核心数目影响,当然也和硬件机器的性能有关。就上面的数据而言,slice的gc时间是远远快于map的,而map存储指针则是最慢的,不推荐。测试代码来自:http://play.golang.org/p/AeHS...
测试代码中的最初数据(作为参考):