我创建了unit64_t到uint64_t的地图。这是我为assesing空间复杂度编写的代码:无序地图占用大量空间
#include <bits/stdc++.h>
#include "sparsehash/internal/sparseconfig.h"
#include "sparsehash/sparse_hash_map"
using namespace std;
int main(int argc, char *argv[]){
std::string input,reference;
while (getline(cin,input)) {
reference += input;
input.clear();
}
cout<<"length of reference = "<<reference.length()<<endl;
unordered_map<uint64_t, uint64_t> m;
//google::sparse_hash_map<uint64_t, pair<short,long>> m;
for (auto it = reference.begin(); it != reference.end(); it++) {
m[it-reference.begin()]= it-reference.begin();
}
return 0;
}
当我跑这跟在/ usr/bin/time会,这是由程序产生的输出:
length of reference = 4641652
Command being timed: "./a.out"
User time (seconds): 2.97
System time (seconds): 0.15
Percent of CPU this job got: 99%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:03.13
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 251816
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 68259
Voluntary context switches: 1
Involuntary context switches: 104
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
无序地图似乎占用了250MB的空间。这似乎是非常高的。为何会发生这种情况。与谷歌稀疏哈希相同的代码只需要89MB的空间,这是更合理的。
我不明白为什么C++无序地图占用这么多空间?
我没有看到任何内容表明内存使用量应该多于或少于250mb。你为什么认为这是很多记忆?你在地图上放置了多少个元素?你有没有考虑到分配填充和地图的内务管理?你有没有考虑给我们提供相关信息? –
是的,输入是一个大小为4.5Mb的字符串。我把每个职位都放在地图上。 – user1995120
@ user1995120:如果你在一个远远小于4GB的字符串中存储位置(最大可寻址32位数字),并且关心内存使用情况,为什么要使用64位值? (如果要保留处理大于4GB字符串的能力,您仍然可以使用模板创建代码的32位和64位版本,并在运行时使用最佳版本。 –