What's the best hashing algorithm to use on a stl string when using hash_map?

前端 未结 11 2333
生来不讨喜
生来不讨喜 2020-12-04 10:41

I\'ve found the standard hashing function on VS2005 is painfully slow when trying to achieve high performance look ups. What are some good examples of fast and efficient has

11条回答
  •  忘掉有多难
    2020-12-04 11:43

    From some old code of mine:

    /* magic numbers from http://www.isthe.com/chongo/tech/comp/fnv/ */
    static const size_t InitialFNV = 2166136261U;
    static const size_t FNVMultiple = 16777619;
    
    /* Fowler / Noll / Vo (FNV) Hash */
    size_t myhash(const string &s)
    {
        size_t hash = InitialFNV;
        for(size_t i = 0; i < s.length(); i++)
        {
            hash = hash ^ (s[i]);       /* xor  the low 8 bits */
            hash = hash * FNVMultiple;  /* multiply by the magic number */
        }
        return hash;
    }
    

    Its fast. Really freaking fast.

提交回复
热议问题