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

前端 未结 11 2316
生来不讨喜
生来不讨喜 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:18

    I worked with Paul Larson of Microsoft Research on some hashtable implementations. He investigated a number of string hashing functions on a variety of datasets and found that a simple multiply by 101 and add loop worked surprisingly well.

    unsigned int
    hash(
        const char* s,
        unsigned int seed = 0)
    {
        unsigned int hash = seed;
        while (*s)
        {
            hash = hash * 101  +  *s++;
        }
        return hash;
    }
    

提交回复
热议问题