简体   繁体   English

如何在 C++ 中近似 std::unordered_map 的大小

[英]How to approximate the size of an std::unordered_map in C++

It is said that an unordered_map<int,int> takes up much more space than a vector<int> .据说unordered_map<int,int>vector<int> > 占用更多的空间。 While I am completely aware of that, I would like to know how to get the approximate size of a single instance of an unordered_map in C++ .虽然我完全意识到这一点,但我想知道如何在C++中获取unordered_map的单个实例的大致大小。 For now, let's say that I inserted n = 1000000 elements into it.现在,假设我在其中插入了n = 1000000元素。 I presume that the memory taken up is n multiplied by some kind of constant, I am, however, unable to find an accurate answer anywhere on the Internet.我假设占用的 memory 是n乘以某种常数,但是,我无法在 Internet 上的任何地方找到准确的答案。
Here is what I'm doing.这就是我正在做的事情。 I'd like to calculate how much memory u_m uses, without writing any code .我想计算memory u_m使用了多少,无需编写任何代码 Is there a way to do that?有没有办法做到这一点?

#include<bits/stdc++.h>
using namespace std;

const int N = 1000000;
unordered_map<int,int> u_m ;

int main(){
     for(int i = 0;i<N;i++){
         u_m[i] = 123+i;
     }
     return 0;
}

If that makes a difference, I intentionally put u_m outside of main如果这有所作为,我故意将u_m放在main之外

There is no general purpose answer to this.对此没有通用的答案。 The memory used can vary wildly based on the implementation.使用的 memory 可能会因实施而异。 To be clear, unordered_map is not tree-based;需要明确的是, unordered_map不是基于树的; it's typically implemented as an array of buckets.它通常被实现为一个桶数组。

But while the spec allows you to know how many buckets are currently in play (via bucket_count ) and you can ask for the number of items in each bucket (with bucket_size ), there is no way to ask how a bucket is implemented.但是,虽然规范允许您知道当前有多少个桶(通过bucket_count )并且您可以询问每个桶中的项目数(使用bucket_size ),但没有办法询问桶是如何实现的。 Based on the various requirements of methods like bucket_size and extract / merge , it's likely a bare bones linked list ( bucket_size is allowed to be O(n) in the size of the bucket, so it needn't know its own size directly; extract needs to be able to return a handle that can be transferred between unordered_map s, and merge is guaranteed not to copy or move when moving elements from one unordered_map to another), but the details of implementation are largely hidden.根据bucket_sizeextract / merge等方法的各种要求,它很可能是一个简单的链表( bucket_size在桶的大小中允许为O(n) ,所以它不需要直接知道自己的大小; extract需要能够返回一个可以在unordered_map之间传输的句柄,并且在将元素从一个unordered_map移动到另一个时, merge保证不会复制或移动),但是实现的细节在很大程度上是隐藏的。

There's also no guarantee on what is stored in the first place.也无法保证首先存储的内容。 It could be just key and value, or key, value and hash, or something else.它可能只是键和值,或者键、值和 hash 或其他东西。

So while you can get basic info from the various .bucket* APIs, since the contents and implementation of a "bucket" is itself essentially unspecified, you'll never get an answer to "how big is an unordered_map " from any C++ standard APIs;因此,虽然您可以从各种.bucket* API 中获取基本信息,但由于“bucket”的内容和实现本身本质上是未指定的,您永远不会从任何 C++ 标准 API 中得到“ unordered_map有多大”的答案; you'd need to know the implementation details and use them alongside the .bucket* APIs, to get an estimate.您需要了解实现细节并将它们与.bucket* API 一起使用,以获得估计。

An unordered_map is not a tree, it is a hash table. unordered_map不是树,它是 hash 表。 It size is dependent on several things, both on amount you've inserted, but also on things like calling reserve to pre-allocate memory.它的大小取决于几个因素,既取决于您插入的数量,也取决于诸如调用reserve以预分配 memory 之类的事情。 The specific settings of initial allcoation size and load factors are implementation dependent, so any guess you make will probably differ between compilers, and the order of operations that result in when and by how much the hash table resizes will differ too.初始分配大小和加载因子的具体设置取决于实现,因此您所做的任何猜测都可能在编译器之间有所不同,并且导致 hash 表调整大小的时间和大小的操作顺序也会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM