How to test a hash function?

前端 未结 4 830
悲&欢浪女
悲&欢浪女 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:54

    Based on your clarification:

    I have used long values in Java in such a way that the first 32 bit encoded an ID and the second 32 bit encoded another ID. Unfortunately Java's hash of long values just XORs the first 32 bit with the second 32 bits, which in my case led to very poor performance when used in a HashMap.

    it appears you have some unhappy "resonances" between the way you assign the two ID values and the sizes of your HashMap instances.

    Are you explicitly sizing your maps, or using the defaults? A QAD check seems to indicate that a HashMap starts with a 16-bucket structure and doubles on overflow. That would mean that only the low-order bits of the ID values are actually participating in the hash bucket selection. You could try using one of the constructors that takes an initial-size parameter and create your maps with a prime initial size.

    Alternately, Dave L's suggestion of defining your own hashing of long keys would allow you to avoid the low-bit-dependency problem.

    Another way to look at this is that you're using a primitive type (long) as a way to avoid defining a real class. I'd suggest looking at the benefits you could achieve by defining the business classes and then implementing hash-coding, equality, and other methods as appropriate on your own classes to manage this issue.

提交回复
热议问题