TVP

# Hash算法那点事

fromhashlibimportmd5

fromstructimportunpack_from

ITEMS =10000000

NODES =100

node_stat = [foriinrange(NODES)]

foriteminrange(ITEMS):

k = md5(str(item)).digest()

h = unpack_from(">I",k)[]

n = h % NODES

node_stat[n] +=1

_ave = ITEMS / NODES

_max =max(node_stat)

_min =min(node_stat)

print("Ave: %d"% _ave)

print("Max: %d(%0.2f%%)"% (_max,(_max - _ave) *100.0/ _ave))

print("Min: %d(%0.2f%%)"% (_min,(_ave - _min) *100.0/ _ave))

Ave: 100000

Max: 100695(0.69%)

Min: 99073(0.93%)

Python代码如下：

fromhashlibimportmd5

fromstructimportunpack_from

ITEMS =10000000

NODES =100

NEW_NODES =101

node = [foriinrange(NODES)]

change =

foriteminrange(ITEMS):

k = md5(str(item)).digest()

h = unpack_from(">I",k)[]

n = h % NODES

n_new = h % NEW_NODES

ifn_new != n:

change +=1

print("Change: %d(%0.2f%%)"% (change,change *100.0/ ITEMS))

Change: 9900989(99.01%)

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180525B1KOIJ00?refer=cp_1026
• 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 cloudcommunity@tencent.com 删除。

2022-12-05

2022-12-05

2022-12-05

2022-12-05

2022-12-05

2022-12-05

2022-12-05

2018-06-01

2022-12-05

2022-12-05

2022-12-05