地图中的最大元素数

编程入门 行业动态 更新时间:2024-10-26 16:27:53
本文介绍了地图中的最大元素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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)

更多推荐

地图中的最大元素数

本文发布于:2023-11-30 05:46:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648923.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:素数   图中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!