Why are hash table expansions usually done by doubling the size?

后端 未结 6 983
小蘑菇
小蘑菇 2020-12-02 06:21

I\'ve done a little research on hash tables, and I keep running across the rule of thumb that when there are a certain number of entries (either max or via a load factor lik

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 07:22

    One reason for doubling size that is specific to hash containers is that if the container capacity is always a power of two, then instead of using a general purpose modulo for converting a hash to an offset, the same result can be achieved with bit shifting. Modulo is a slow operation for the same reasons that integer division is slow. (Whether integer division is "slow" in the context of whatever else is going in a program is of course case dependent but it's certainly slower than other basic integer arithmetic.)

提交回复
热议问题