Testing

go map,slice,array 遍历性能测试

golang testing about iterating map,slice,array.

qwding

2 minute read

#就是随便试试 听说goalng的map和slice的遍历性能差的不是一点半点,于是没事写个benchmark来玩玩 代码 package testings import( "testing" "strconv" ) var length = 1000 var maps map[string]string var slices []string var arrays [1000]string func init(){ maps = make(map[string]string,length) slices = make([]string,length) for i:=0;i<length;i++{ maps[strconv.Itoa(i)] = "abc" slices[i] = "abc" arrays[i] = "abc" } } func BenchmarkIterateMap(b *testing.B){ for i:=0;i<b.N;i++{ for , = range maps{ } } } func BenchmarkIterateSlices(b *testing.B){ for i:=0;i<b.N;i++{ for , = range slices{ } } } func BenchmarkIterateArrays(b *testing.