简体   繁体   English

slow std :: map for large entries

[英]slow std::map for large entries

We have 48,16,703 entries in this format. 我们有48,16,703个这种格式的条目。

1 abc
2 def
...
...
4816702 blah
4816703 blah_blah

Since the number of entries are quite big, I am worried that std::map would take much time during insertion since it need to do the balancing as well for each insertion. 由于条目数量非常大,我担心std :: map在插入过程中会花费很多时间,因为它需要为每次插入进行平衡。

Only inserting these entries into the map takes a lot of time. 只将这些条目插入地图需要花费大量时间。 I am doing 我在做

map[first] = second;

Two questions: 1. Am I correct in using std::map for these kind of cases? 两个问题:1。我是否正确使用std :: map来处理这类案件? 2. Am I correct in inserting like the above way. 我是否正确插入如上所述。 OR Should I use map.insert() 或者我应该使用map.insert()

I am sorry for not doing the experiments and writing the absolute numbers but we want an general consensus if we are doing the right thing or not. 我很抱歉没有做实验并写出绝对数字,但如果我们做的是正确的话,我们希望得到普遍的共识。

Also, they keys are not consecutive always.. 此外,他们的钥匙不是连续的..

PS Of-course, later we will need to access that map as well to get the values corresponding to the keys. PS当然,稍后我们还需要访问该地图以获取与键对应的值。

If you don't need to insert into the map afterwards, you can construct an unsorted vector of your data, sort it according to the key, and then search using functions like std::equal_range . 如果之后不需要插入地图,则可以构造数据的未排序向量,根据键对其进行排序,然后使用std::equal_range等函数进行搜索。
It's the same complexity as std::map , but far less allocations. 它与std::map复杂性相同,但分配的次数要少得多。

Use an std::unordered_map , which has much better insertion time complexity than std::map , as the reference mentions: 使用std::unordered_map ,其插入时间复杂度比std::map好得多,如引用所示:

Complexity

Single element insertions:
    Average case: constant.
    Worst case: linear in container size.

Multiple elements insertion:
    Average case: linear in the number of elements inserted.
    Worst case: N*(size+1): number of elements inserted times the container size plus one.

May trigger a rehash (not included in the complexity above).

That's better than the logarithmic time complexity of std::map 's insertion. 这比std::map插入的对数时间复杂度要好。

Note: std::map 's insertion can enjoy " amortized constant if a hint is given and the position given is the optimal.". 注意:如果给出提示并且给出的位置是最佳的,则std::map的插入可以享受“ 摊销常数”。 If that's the case for you, then use a map (if a vector is not applicable). 如果是这种情况,那么使用地图(如果矢量不适用)。

@nm provides a representative Live demo @nm提供了一个代表性的现场演示

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

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