Why is the size 127 (prime) better than 128 for a hash-table?

后端 未结 9 1294
深忆病人
深忆病人 2021-01-30 08:22

Supposing simple uniform hashing, that being, any given value is equally like to hash into any of the slots of the hash. Why is it better to use a table of size 127 and not 128?

相关标签:
9条回答
  • 2021-01-30 09:14

    All numbers (when hashed) are still going to be the p lowest-order bits of k for 127 too.

    That is wrong (or I misunderstood..). k % 127 depends on all bits of k. k % 128 only depends on the 7 lowest bits.


    EDIT:

    If you have a perfect distribution between 1 and 10,000. 10,000 % 127 and 10,000 % 128 both will turn this in a excellent smaller distribution. All buckets will contain 10,000 /128 = 78 (or 79) items.

    If you have a distribution between 1 and 10,000 that is biased, because {x, 2x, 3x, ..} occur more often. Then a prime size will give a much, much better distribution as explained in this answer. (Unless x is exactly that prime size.)

    Thus, cutting off the high bits (using a size of 128) is no problem whatsoever if the distribution in the lower bits is good enough. But, with real data and real badly designed hash functions, you will need those high bits.

    0 讨论(0)
  • 2021-01-30 09:14

    If you have a perfect hash function that has an even distribution, then it doesn't matter.

    0 讨论(0)
  • 2021-01-30 09:20

    I cannot prove it anymore, although I remember having to do so in an exam at university a million years ago, but optimal hash sizes are not merely prime. You want to pick a prime number N such that N = 4*M − 1 (where M is also an integer).

    That makes 31 a better number of buckets than 29. M is 8 when N is 31, but there is no integral M when N is 29.

    As I said, I no longer remember the math to prove this. It was in a theory course taught by Rachel Manber, Udi’s wife, about 25 years ago or so.

    0 讨论(0)
提交回复
热议问题