Choosing a multiplier for a (string) hash function

后端 未结 3 2045
小鲜肉
小鲜肉 2021-02-05 14:03

Do you have any advice/rules on selecting a multiplier to use in a (multiplicative) hash function. The function is computing the hash value of a string.

3条回答
  •  太阳男子
    2021-02-05 14:33

    I had an interesting discussion with a coworker about hash function recently. Our conclusions were as follows:

    If you really need to write a good hash function that minimizes collisions more than the default implementations available in the standard languages you need an advanced degree in mathematics.

    If you're writing applications where a custom hash function will noticeably improve the performance of your application, you're Google and you've got plenty of Math PhDs to do the work.

    Sorry to not directly answer your question, but the bottom line is that there's really no need to write your own hash function for String. What language are you working with? I'd imagine there's an easy way to compute a "good enough" hash code.

提交回复
热议问题