I have some vector of integer that I would like to store efficiently in a unordered_map in c++11 my question is this:
How do I best store these and optimize for .find
queries?
I came up with the following hasher:
class uint32_vector_hasher {
public:
std::size_t operator()(std::vector<uint32_t> const& vec) const {
std::size_t ret = 0;
for(auto& i : vec) {
ret ^= std::hash<uint32_t>()(i);
}
return ret;
}
};
and then store the objects in an unordered_map
I do however have a couple of questions
- how often does the hash get calculated, only one, some random number or times?
- Would it make sense to create a wrapper object with
==
and hash functions to make memorize the hash and avoid it being calculated more than once?
When profiling I've noticed that a rather large amount of my cpu time is spend doing lookups on the unordered maps, this is not exactly optimal :(
question from:
https://stackoverflow.com/questions/20511347/a-good-hash-function-for-a-vector 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…