5

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

returneax
  • 709
  • 1
  • 4
  • 18
  • 2
    i think the c++0x standard doesn't specify an implementation, so it will be quite difficult to benchmark. are you actually asking for specific implementations of stl? – lijie Dec 11 '10 at 01:35
  • 1
    And isn't STL's unordered map one of the features imported from Boost to C++ standard? – Kos Dec 11 '10 at 01:39
  • 2
    The c++0x unordered_map is not based on the boost library, it is based on the TR1 unordered_map which was defined before the implementation in the boost library. – hmuelner Dec 13 '10 at 13:28

4 Answers4

5

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.

alexk7
  • 2,721
  • 1
  • 21
  • 18
  • Yes. For portability, see http://stackoverflow.com/questions/724465/how-to-check-for-tr1-while-compiling – alexk7 Dec 15 '10 at 17:55
2

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.

user534498
  • 3,926
  • 5
  • 27
  • 52
  • It's in the `boost` namespace unless you explicitly ask for the `tr1` one. – Billy ONeal Dec 11 '10 at 03:00
  • Should I use the boost one for the best performance as of now? – returneax Dec 11 '10 at 09:37
  • 5
    You shouldn't make any choices based on expected performance now. You should use whichever one is most convenient and then look for optimization opportunities after you have profiled your application and found the `unordered_map` implementation to be a bottleneck – SingleNegationElimination Dec 11 '10 at 15:55
  • Well implementing either of them would be just as easy really. It will be used for queries almost constantly so I imagine efficiency will be necessary and why not have the best? – returneax Dec 12 '10 at 21:24
  • `C++0x unordered_map is proposed based on boost one.` No, both `std::unordered_map` and `boost::unordered_map` were based on the LWG's reference `tr1::unordered_map`. – underscore_d Jul 29 '16 at 11:21
2

One minor point not yet mentioned, the std::hash function is only required to be able to compute hashes of builtin types and strings (and a few other types). The boost::hash function can compute hashes of more complex objects such as pair and tuple. Also boost has a hash_combine function to assist in creating hashes for user-defined types.

This means that std::unordered_set< pair<int, int> > won't compile, but boost::unordered_set< pair<int, int> > will.

You can use boost::hash with std::unordered_* if needed.

(Reference: Item 6.18 in the Library Extension Technical Report Issues List.)

AFoglia
  • 7,968
  • 3
  • 35
  • 51
  • I have found that this turns out to be very important information in practical terms. You inevitably find yourself dealing with your own classes for which you need to cook up hash functions. So even in 2014 I am sticking with boost::unordered_set when storing hashmaps of my own classes, due to the ability to easily create hash functions. – moodboom Jan 06 '14 at 17:05
1

It depends on the implementation and the data set in question. When I was playing around with unordered_map for a blog post I found that VS10's std::unordered_map perfromed much worse than boost::unordered_map for the input I used (I didn't build a thorough benchmark). In theory thought there shouldn't be a difference.

Motti
  • 110,860
  • 49
  • 189
  • 262