地图中的最大元素数 [英] Maximum number of elements in map

查看:93
本文介绍了地图中的最大元素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GO中的地图中最多可以存储多少个元素?如果我需要经常访问Map中的数据,那么在长时间运行的程序中,继续向Map中添加项目并从中检索是一个好主意吗?

What is the maximum number of elements that can be stored in a Map in GO? If I need to access data from Map frequently, is it a good idea to keep on adding items to Map and retrieving from it, in a long running program?

推荐答案

除了map-length类型的最大值为int之外,映射中的元素数量没有理论上的限制. int的最大值取决于要编译的目标体系结构,如果是32位,则可能是1 << 31 - 1 = 2147483647,如果是64位,则可能是1 << 63 - 1 = 9223372036854775807.

There is no theoretical limit to the number of elements in a map except the maximum value of the map-length type which is int. The max value of int depends on the target architecture you compile to, it may be 1 << 31 - 1 = 2147483647 in case of 32 bit, and 1 << 63 - 1 = 9223372036854775807 in case of 64 bit.

请注意,作为实施限制,您可能无法完全添加max-int元素,但是数量级将相同.

Note that as an implementation restriction you may not be able to add exactly max-int elements, but the order of magnitude will be the same.

由于内置的​​map类型使用哈希图实现,因此访问时间复杂度通常为O(1),因此将许多元素添加到映射中是完全可以的,您仍然可以非常快速地访问元素.请注意,但是添加许多元素将导致内部结构的重新哈希和重建,这将需要一些额外的计算-在向地图添加新键时可能偶尔发生.

Since the builtin map type uses a hashmap implementation, access time complexity is usually O(1), so it is perfectly fine to add many elements to a map, you can still access elements very fast. Note that however adding many elements will cause a rehashing and rebuilding the internals, which will require some additional calculations - which may happen occasionally when adding new keys to the map.

如果您可以猜测"或估计地图的大小,则可以创建具有较大容量的地图以避免重新散列.例如.您可以创建一个具有一百万个元素的空间的地图,如下所示:

If you can "guess" or estimate the size of your map, you can create your map with a big capacity to avoid rehashing. E.g. you can create a map with space for a million elements like this:

m := make(map[string]int, 1e6)

这篇关于地图中的最大元素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆