Difference in performance between map and unordered_map in c++

后端 未结 3 1996
借酒劲吻你
借酒劲吻你 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 03:03

    Insertion for unordered_map should be O(1) and retrieval should be roughly O(1), (its essentially a hash-table).

    Your timings as a result are way OFF, or there is something WRONG with your implementation or usage of unordered_map.

    You need to provide some more information, and possibly how you are using the container.

    As per section 6.3 of n1836 the complexities for insertion/retreival are given:

    • http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf

    One issue you should consider is that your implementation may need to continually be rehashing the structure, as you say you have 100mil+ items. In that case when instantiating the container, if you have a rough idea about how many "unique" elements will be inserted into the container, you can pass that in as a parameter to the constructor and the container will be instantiated accordingly with a bucket-table of appropriate size.

提交回复
热议问题