golang map是golang的方便操作的key-value
package main import ( "fmt" "math/rand" "time" _"strconv" ) var x = make(map[int]int, 100000001) //var x = make(map[string]string, 100000001) func main() { ttime := time.Now().UnixNano() //x := make(map[int]int, 100000001) //x := [100000001]int{} fmt.Printf("len x %d \n", len(x)) for i := 1; i < 100000001; i++ { x[rand.Intn(i)] = rand.Intn(i) //x[strconv.Itoa(rand.Intn(i))] = "aaaaaaaaaa" } fmt.Printf("len1 x %d \n", len(x)) ttime1 := time.Now().UnixNano() fmt.Printf("time cal %f\n", float64(ttime1-ttime)/float64(1*time.Second)) } // go version :go1.4 操作系统:win7 内存:8G 核数:4
总结:
1、测试结果 make(map[int]int, 100000001-1亿) 大概需要2s,相当于申请100MB空间(估算)
2、1亿次随机写入map[int]int,花费25s时间,大概随机写入5000w个key,平均1s写入400w map[int]int
1亿次随机写入map[string]string,花费38s时间,大概随机写入5000w个key,平均1s写入260w map[string]string