best way: according to the length of the map, create a new array, walk through the map one by one press
method 1:
func getKeys1(m map[int]int) []int {
// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率较高
j := 0
keys := make([]int, len(m))
for k := range m {
keys[j] = k
j++
}
return keys
}
method 2:
func getKeys2(m map[int]int) []int {
// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率较高
keys := make([]int, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
other methods:
method 3:
func getKeys3(m map[int]int) []int {
// 注意:由于数组默认长度为0,后面append时,需要重新申请内存和拷贝,所以效率较低
keys := []int{}
for k := range m {
keys = append(keys, k)
}
return keys
}
method 4:
func getKeys4(m map[int]int) int {
// 注意:虽然此写法简洁,但MapKeys函数内部操作复杂,效率极低
keys := reflect.ValueOf(m).MapKeys()
return len(keys)
}
experimental results are shown in the figure (you can see that methods 1 and 2 have the highest efficiency and least memory operation) :
complete code as follows:
package test
import (
"reflect"
"testing"
)
// 初始化map
func initMap() map[int]int {
m := map[int]int{}
for i := 0; i < 10000; i++ {
m[i] = i
}
return m
}
func getKeys1(m map[int]int) []int {
// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率较高
j := 0
keys := make([]int, len(m))
for k := range m {
keys[j] = k
j++
}
return keys
}
func getKeys2(m map[int]int) []int {
// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率较高
keys := make([]int, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
// 初始化默认
func getKeys3(m map[int]int) []int {
// 注意:由于数组默认长度为0,后面append时,需要重新申请内存和拷贝,所以效率较低
keys := []int{}
for k := range m {
keys = append(keys, k)
}
return keys
}
// 使用反射
func getKeys4(m map[int]int) int {
// 注意:虽然此写法简洁,但MapKeys函数内部操作复杂,效率极低
keys := reflect.ValueOf(m).MapKeys()
return len(keys)
}
func BenchmarkMapkeys1(b *testing.B) {
// 初始化map
m := initMap()
b.ResetTimer()
for i := 0; i < b.N; i++ {
getKeys1(m)
}
}
func BenchmarkMapkeys2(b *testing.B) {
// 初始化map
m := initMap()
b.ResetTimer()
for i := 0; i < b.N; i++ {
getKeys2(m)
}
}
func BenchmarkMapkeys3(b *testing.B) {
// 初始化map
m := initMap()
b.ResetTimer()
for i := 0; i < b.N; i++ {
getKeys3(m)
}
}
func BenchmarkMapkeys4(b *testing.B) {
// 初始化map
m := initMap()
b.ResetTimer()
for i := 0; i < b.N; i++ {
getKeys4(m)
}
}
p>
div>
Read More:
- How to compare the equality of structures in golang
- How to generate and view SSH keys in Ubuntu 16.04
- Java – read all the files and folders in a certain directory and three methods to get the file name from the file path
- Map to vector pair map.second sort
- An error occurred trying to connect: get http: / / var / run/ docker.sock/v1 .21/containers/json?all
- How to solve the problem of failed installation of golang plug-in dependency in vscode under Windows
- [go] solve the fatal error of go: concurrent map writes map non concurrent security
- #An error is reported by the chart map component of renfast framework
- How to Solve All masterha_check_repl Error
- How to use C # to get image format without system. Drawing. Common
- Error handling when ABP specifies map object during map operation
- Easynvr operation log reports an error. Fatal error: concurrent map read and map write troubleshooting
- Run with Python console solution is set by default for all projects in pcharm
- Android 9 (P) recovery upgrade Map of ‘@/cache/recovery/block.map’ failed problem analysis guide
- How to Use Apt get Command Under Mac OSX
- Quickly convert map to ordered array
- JQuery: How to get the selected values of checkbox, radio and select
- Net Q & A: how to avoid the exception thrown by max() on emptyenumerable?
- Golang gets the list of files under the folder
- Duplicate keys detected: ‘***‘. This may cause an update error