Difference in performance between map and unordered_map in c++

后端 未结 3 1976
借酒劲吻你
借酒劲吻你 2020-12-30 02:07

I have a simple requirement, i need a map of type . however i need fastest theoretically possible retrieval time.

i used both map and the new proposed unordered_map

3条回答
  •  生来不讨喜
    2020-12-30 02:52

    unordered_map (at least in most implementations) gives fast retrieval, but relatively poor insertion speed compared to map. A tree is generally at its best when the data is randomly ordered, and at its worst when the data is ordered (you constantly insert at one end of the tree, increasing the frequency of re-balancing).

    Given that it's ~10 million total entries, you could just allocate a large enough array, and get really fast lookups -- assuming enough physical memory that it didn't cause thrashing, but that's not a huge amount of memory by modern standards.

    Edit: yes, a vector is basically a dynamic array.

    Edit2: The code you've added some some problems. Your while (! LabelFile.eof() ) is broken. You normally want to do something like while (LabelFile >> inputdata) instead. You're also reading the data somewhat inefficiently -- what you apparently expecting is two numbers separated by a tab. That being the case, I'd write the loop something like:

    while (LabelFile >> node >> label)
        Label[node] = label;
    

提交回复
热议问题