```go
package main
import (
"fmt"
"time"
)
func main() {
const nums = 1000000
arr := [nums]int{}
ma := make(map[int]int, nums)
start := time.Now()
for i := 0; i < nums; i++ {
arr[i] = i
}
fmt.Printf("add arr %.2f s", time.Since(start).Seconds())
fmt.Println("-------------------------")
start1 := time.Now()
for i := 0; i < nums; i++ {
ma[i] = i
}
fmt.Printf("add map %.2f s", time.Since(start1).Seconds())
fmt.Println("-------------------------")
start2 := time.Now()
for _, _ = range arr {
//fmt.Print(k, v)
}
fmt.Println("-------------------------")
fmt.Printf("for each array %.2f s\n", time.Since(start2).Seconds())
fmt.Println("-------------------------")
start3 := time.Now()
for _,_= range ma {
}
fmt.Println("-------------------------")
fmt.Printf("for ecah map %.2f s\n", time.Since(start3).Seconds())
fmt.Println("-------------------------")
}
````
打印的结果是 nums越大的时候,map的时间就比array多了,
![image.png](https://static.studygolang.com/180802/f6e35f538b2b2e3cf498b2662dac0899.png)
但是查看资料说是:
![image.png](https://static.studygolang.com/180802/188d4ad2eefe355bc3bb0598fadbeb74.png)
##那到底哪个效率高些呢
有疑问加站长微信联系(非本文作者)