How to I count key collisions when using boost::unordered_map?
I have a data structure with 15 unsigned longs, I have defined a hash function using hash_co开发者_运维技巧mbine as follows:
friend std::size_t hash_value(const TUPLE15& given)
{
std::size_t seed = 0;
boost::hash_combine(seed, val1);
boost::hash_combine(seed, val2);
...
return seed;
}
I insert a large number of values into a boost::unordered_map but the performance is not good enough. Probably, I could do better with an alternative hashing function. To confirm this, I need to check how many collisions I am getting. How do I do this?
How about comparing the number of tuples vs. the number of unique hash values?
set<size_t> hash_values;
BOOST_FOREACH(const TUPLE15& tuple, tuples)
hash_values.insert(hash_value(tuple));
size_t collisions = tuple_map.size() - hash_values.size();
or
size_t collisions = 0;
for (size_t bucket = 0; bucket != tuples.bucket_count(); ++bucket)
if (tuples.bucket_size(bucket) > 1)
collisions += tuples.bucket_size(bucket) - 1;
精彩评论