C++ 09 Jul 2009 23:43:35
C++ Map Speeds, GNU g++ Edition
(There is also an MSVC++ Edition of these performance tests, and a newer std::set comparison.)
While testing whether anything would break by switching to std::tr1::unordered_map for CG-3, I noticed it ran a consistent 15% faster. So, for my own curiousity, I threw together this little speed test: timemap.cpp with cycle.h.
Should be compiled with GCC g++ of at least version 4.3 to have TR1, and with -O2 or higher; -O will not optimize the hash maps sufficiently.
Now, that is obviously a naive test. Maps of integer to integer is a trivial case, but it is also a case I use often in CG-3 so I figured it would suit fine for a test. Results are as follows, with std::map set as reference 100% and lower is better.
<int,int> | Insert | Lookup | Iterate | Erase |
---|---|---|---|---|
std::map | 100.00% | 100.00% | 100.00% | 100.00% |
__gnu_cxx::hash_map | 26.86% | 5.40% | 62.71% | 16.87% |
std::tr1::unordered_map | 36.68% | 4.92% | 37.70% | 15.93% |
boost::unordered_map | 28.55% | 5.27% | 36.03% | 15.40% |
And the corresponding test for std::string as key, again std::map set to 100% and lower is better:
<std::string,int> | Insert | Lookup | Iterate | Erase |
---|---|---|---|---|
std::map | 100.00% | 100.00% | 100.00% | 100.00% |
__gnu_cxx::hash_map | 31.77% | 45.88% | 65.19% | 34.35% |
std::tr1::unordered_map | 48.85% | 31.55% | 165.41% | 24.94% |
boost::unordered_map | 33.36% | 17.96% | 64.94% | 13.91% |