I wrote some code to test my unordered map performance with a 2 component vector as a key.
std::unordered_map<Vector2i, int> m;                                                                      
for(int i = 0; i < 1000; ++i)                                                                             
    for(int j = 0; j < 1000; ++j)                                                                         
        m[Vector2i(i,j)] = i*j+27*j;                                                                      
clock.restart();                                                                                          
auto found = m.find(Vector2i(0,5));                                                                                                                                                            
std::cout << clock.getElapsedTime().asMicroseconds() << std::endl;                                         
output for the code above: 56 (microseconds) When I replace 1000 in the for loops by 100 the outputs is 2 (microseconds) Isn't the time supposed to be constant ?
hash function for my Vector2i:
namespace std                                                                                                    
{
   template<>                                                                                                   
    struct hash<Vector2i>                                                                                        
    {                                                                                                            
        std::size_t operator()(const Vector2i& k) const                                                          
        {                                                                                                        
            using std::size_t;                                                                                   
            using std::hash;                                                                                     
            using std::string;                                                                                   
            return (hash<int>()(k.x)) ^ (hash<int>()(k.y) << 1);                                                 
        }                                                                                                        
    };                                                                                                           
}                                                                             
EDIT: I added this code to count the collisions after the for loop:
for (size_t bucket = 0; bucket != m.bucket_count(); ++bucket)                                             
    if (m.bucket_size(bucket) > 1)                                                                        
         ++collisions; 
With 100*100 elements: collisions = 256
1000*1000 elements: collisions = 2048
Aucun commentaire:
Enregistrer un commentaire