1

我有一个 uint64_t 到向量的映射。对于大小为 4MB 的字符串输入,与 unordered_map(~4s) 相比,该映射需要非常长的时间(~70s)来构建。同样,对于大小约为 150MB 的字符串输入,120 秒与 2400 秒。

Unordered_map 一般比 google sparse hash 快,但是时间上的差别很大。对于从 int 到 int 的映射,稀疏哈希比无序映射慢约 2 倍。

我不明白这个时差背后的原因。

代码片段:

int main(){
std::string input,reference;

while (getline(cin,input)) {
    reference += input;
    input.clear();
}

cout<<"length of reference = "<<reference.length()<<endl;

google::sparse_hash_map<uint64_t, vector<uint32_t>> m;
//unordered_map<uint64_t, vector<uint32_t>> m;
for (auto it = reference.begin(); it != reference.end(); it++) {
    m[it-reference.begin()].push_back(it-reference.begin());
}
return 0;
}

这是程序上 /usr/bin/time 的结果

length of reference = 4641652
    Command being timed: "./a.out"
    User time (seconds): 76.06
    System time (seconds): 0.31
    Percent of CPU this job got: 99%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 1:16.43
    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): 308228
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 230585
    Voluntary context switches: 1
    Involuntary context switches: 2316
    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
4

0 回答 0