python之路[24] - 算法 - 迁

[一] 布隆过滤器

然后我们假设你的网站已经被1亿个用户访问过,每个ip的长度是15,那么你一共需要15 * 100000000 = 1500000000Bytes = 1.4G,这还没考虑hash冲突的问题(hash表中的槽位越多,越浪费空间,槽位越少,效率越低)。
于是聪明的你稍一思考,又想到可以把ip转换成无符号的int型值来存储,这样一个ip只需要占用4个字节就行了,这时1亿个ip占用的空间是4 * 100000000 = 400000000Bytes = 380M,空间消耗降低了很多。
数据通过-->fun(1-n)-->定位到槽位

通过一个哈希序列来快速定位是与否在过滤器里面

pip2 install bloom-filter

  

from bloom_filter import BloomFilter

bloom = BloomFilter(max_elements=10000, error_rate=0.1)

# Test whether the bloom-filter has seen a key:
print "test-key" in bloom

# Mark the key as seen
bloom.add("test-key")

# Now check again
print "test-key" in bloom

>>>>>>
False
True

  

 

 

[二] 一致性哈希

 

[三] 冒泡查询

posted @ 2019-01-09 04:30  richardzgt  阅读(181)  评论(0编辑  收藏  举报