Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the C++0x standard there will be unordered_map, how does this compare to boosts unordered_map?

Which is more efficient? Are there any good benchmarks out?

like image 984
returneax Avatar asked Dec 11 '10 01:12

returneax


People also ask

What is the difference between unordered_map and Unordered_set?

The difference between an unordered_map and an unordered_set is that an unordered_map stores data only in the form of key-value pair while an unordered_set can store data that is not necessarily in the form of key-value pairs (example integer, string, etc.).

What are the advantages of an unordered_map over a map?

Use std::unordered_map when You need to keep count of some data (Example – strings) and no ordering is required. You need single element access i.e. no traversal.

What is faster than unordered_map?

Insertion of spread keys in std::map tends to outperform std::unordered_map when map size is under 10000 elements. Insertion of dense keys in std::map doesn't present performance difference with std::unordered_map under 1000 elements. In all other situations std::unordered_map tends to perform faster.

Which is faster map or unordered_map?

As you can see, using the unordered_map is substantially faster than the map implementation, even for small numbers of elements.


2 Answers

C++11's std::unordered_map specification is similar to boost::unordered_map which is based on tr1::unordered_map. That being said, there are some small differences. The addition of rvalue references in C++11 result in the addition of the emplace and emplace_hint functions that may be useful for performance.

C++11 is now widely implemented and so you should be able to use std::unordered_map out of the box. C++14 does not change it significantly and C++17 will (probably) add the insert_or_assign and try_emplace member functions.

like image 100
alexk7 Avatar answered Sep 18 '22 17:09

alexk7


In the c++0x latest standard draft n3225, there's a section 23.6.1 class template unordered_map.

So it is already there.

C++0x unordered_map is proposed based on boost one. Boost library itself also has a namespace tr1::unordered_map, which shares the implementation of its own boost::unordered_map.

If you want to compare (of course you don't need to compare boost with boost), I think several other compilers, including microsoft visual studio 2010, and gcc, do have their own unordered_map implementation. You can use them by assuming they are under namespace tr1.

#include <unordered_map>
...
std::tr1::unordered_map<...>

I didn't know any benchmark yet but I think at this early time, any benchmarking doesn't make sense because the compiler implementer will definitely optimize their own implementations when the real standard is finalized and more people are going to use the library.

like image 38
user534498 Avatar answered Sep 20 '22 17:09

user534498