bloomFilter_demo

参考博客:(14条消息) 布隆(Bloom Filter)过滤器入门_布隆过滤器入门_qq_39093474的博客-CSDN博客

5 分钟搞懂布隆过滤器,亿级数据过滤算法你值得拥有! - 知乎 (zhihu.com)

BloomFilterTest.java

package com.hmb;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

import java.util.HashSet;
import java.util.Set;

public class BloomFilterTest {
    private static Integer EXPECTED_INSERT_NUM = 10000000;
    private static double fpp = 0.01;

    private static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(),
            EXPECTED_INSERT_NUM, fpp);

    private static Set<Integer> set = new HashSet<>();
    public static void main(String[] args) {
        long stime = System.currentTimeMillis();
        for (int i = 0; i < EXPECTED_INSERT_NUM; i++) {
            bloomFilter.put(i);
        }

        int count = 0;
        for (int i = EXPECTED_INSERT_NUM; i < EXPECTED_INSERT_NUM * 2; i++) {
            if (bloomFilter.mightContain(i)) {
                count++;
            }
        }
        long etime = System.currentTimeMillis();
        System.out.println("incorrect judge num: " + count);
        System.out.println("spend time: " + (etime - stime) + " ms");

        stime = System.currentTimeMillis();
        for (int i = 0; i < EXPECTED_INSERT_NUM; i++) {
            set.add(i);
        }
        count = 0;
        for (int i = EXPECTED_INSERT_NUM; i < EXPECTED_INSERT_NUM * 2; i++) {
            if (set.contains(i)) {
                count++;
            }
        }
        etime = System.currentTimeMillis();
        System.out.println("incorrect judge num: " + count);
        System.out.println("spend time: " + (etime - stime) + " ms");
    }
}

  

笔者拿bloomFilter跟set进行了对比,当EXPECTED_INSERT_NUM<=10000000时,发现都是set更快,当EXPECTED_INSERT_NUM==100000000时,set就会报OOM了

 

posted @ 2023-06-04 21:23  hemeiwolong  阅读(6)  评论(0编辑  收藏  举报