How to test a hash function?

前端 未结 4 827
悲&欢浪女
悲&欢浪女 2021-02-07 00:01

Is there a way to test the quality of a hash function? I want to have a good spread when used in the hash table, and it would be great if this is verifyable in a unit test.

4条回答
  •  盖世英雄少女心
    2021-02-07 00:45

    If your using a chaining hash table, what you really care about is the number of collisions. This would be trivial to implement as a simple counter on your hash table. Every time an item is inserted and the table has to chain, increment a chain counter. A better hashing algorithm will result in a lower number of collisions. A good general purpose table hashing function to check out is: djb2

提交回复
热议问题