Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unordered_map: which one is faster find() or count()?

What is the fastest way to figure out if an unordered_map container has an item with a specified key?

like image 442
Kimi Avatar asked Jan 04 '13 15:01

Kimi


People also ask

What is the complexity of find in unordered_map?

Return values: If the given key exists in unordered_map it returns an iterator to that element otherwise it returns the end of the map iterator. // find function in unordered_map. Time Complexity : O(1) on average.

What is faster than unordered_map?

For the unordered_map + map , it takes 70 ms for unordered_map insertion and 80 ms for map insertion. So the hybrid implementation is 50 ms faster.

How do I make my unordered map faster?

How to test unordered_map is faster than map ? Note1: Let your hash function H(V) , it is better that H(V) returns distinct value for every distinct V , it makes unordered_map faster; but if you can't do that it doesn't problem.

What does count do in unordered_map?

The unordered_map::count() is a builtin method in C++ which is used to count the number of elements present in an unordered_map with a given key.


1 Answers

They will have about equal performance. You should use the algorithm that best expresses what you are trying to do.

To elaborate on that, generally count() will be implemented using find(). For example, in libcxx, count() is implemented as return (find(__k) != end());

like image 52
Bill Lynch Avatar answered Sep 21 '22 14:09

Bill Lynch