HashMap和布隆过滤器命中性能测试
package datafilter;
import com.google.common.base.Stopwatch;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import java.util.*;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
public class WarnIp {
public static String getRandomIp() {
// 需要排除监控的ip范围
int[][] range = { { 607649792, 608174079 }, // 36.56.0.0-36.63.255.255
{ 1038614528, 1039007743 }, // 61.232.0.0-61.237.255.255
{ 1783627776, 1784676351 }, // 106.80.0.0-106.95.255.255
{ 2035023872, 2035154943 }, // 121.76.0.0-121.77.255.255
{ 2078801920, 2079064063 }, // 123.232.0.0-123.235.255.255
{ -1950089216, -1948778497 }, // 139.196.0.0-139.215.255.255
{ -1425539072, -1425014785 }, // 171.8.0.0-171.15.255.255
{ -1236271104, -1235419137 }, // 182.80.0.0-182.92.255.255
{ -770113536, -768606209 }, // 210.25.0.0-210.47.255.255
{ -569376768, -564133889 }, // 222.16.0.0-222.95.255.255
};
Random rdint = new Random();
int index = rdint.nextInt(10);
String ip = num2ip(range[index][0] + new Random().nextInt(range[index][1] - range[index][0]));
return ip;
}
/*
* 将十进制转换成IP地址
*/
public static String num2ip(int ip) {
int[] b = new int[4];
String x = "";
b[0] = (int) ((ip >> 24) & 0xff);
b[1] = (int) ((ip >> 16) & 0xff);
b[2] = (int) ((ip >> 8) & 0xff);
b[3] = (int) (ip & 0xff);
x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]);
return x;
}
private static Map<String, Integer> map = new HashMap<>();
private static BloomFilter<String> bloomFilter = BloomFilter.create(Funnels.unencodedCharsFunnel(), 1000000);
private static List<String> list = new ArrayList<>();
static {
int count = 100000000;
for (int id = 0; id < count; id++) {
String randomIp = getRandomIp();
int randomPort = (int)(Math.random() * 65536);
String key = randomIp + ":" + randomPort;
if(!map.containsKey(key)) {
System.out.println("key: " + key);
map.put(key, id);
list.add(key);
bloomFilter.put(key);
}
if(map.size() >= 1000000) {
break;
}
}
}
static Stopwatch stopwatch = Stopwatch.createUnstarted();
public static void main(String[] args) {
System.out.println("map.size: " + map.size());
Scanner scanner = new Scanner(System.in); //String param = "222.17.204.104:8001";
while(scanner.hasNextLine()) {
String param = scanner.nextLine();
System.out.println("Input: " + param);
if("exit".equalsIgnoreCase(param)) {
break;
}
stopwatch.start();
// if (map.containsKey(param)) { //map判断
// if(list.contains(param)) {
if (bloomFilter.mightContain(param)) { //布隆过滤器判断(默认3%误差率)
System.out.println("list集合存在该数据=============数据: " + param);
}
//计时结束
stopwatch.stop();
System.out.println("list集合测试,判断该元素集合中是否存在用时: " + stopwatch.elapsed(MILLISECONDS));
System.out.println("list集合测试,判断该元素集合中是否存在用时: " + stopwatch.elapsed(NANOSECONDS));
stopwatch.reset();
}
scanner.close();
}
}
结论:100w数据,key都是字符串(ip:port),map测试时,key存在大概耗时0.01-0.1ms,不存在时大概0.005ms左右,list耗时20-40ms,布隆过滤器0.01-0.2ms左右。除此之外需要考虑占用内存,按255.255.255.255:65535来考虑,用list存储100w条21字节*1000000约20M,布隆过滤器占用更小。
HashMap和布隆过滤器命中性能测试的更多相关文章
- 布隆过滤器的概述及Python实现
布隆过滤器 布隆过滤器是一种概率空间高效的数据结构.它与hashmap非常相似,用于检索一个元素是否在一个集合中.它在检索元素是否存在时,能很好地取舍空间使用率与误报比例.正是由于这个特性,它被称作概 ...
- 浅析布隆过滤器及实现demo
布隆过滤器 布隆过滤器(Bloom Filter)是一种概率空间高效的数据结构.它与hashmap非常相似,用于检索一个元素是否在一个集合中.它在检索元素是否存在时,能很好地取舍空间使用率与误报比例. ...
- 基于Java实现简化版本的布隆过滤器
一.布隆过滤器: 布隆过滤器(Bloom Filter)是1970年由布隆提出的.它实际上是一个很长的二进制向量和一系列随机映射函数.布隆过滤器可以用于检索一个元素是否在一个集合中.它的优点是空间效率 ...
- 算法初级面试题05——哈希函数/表、生成多个哈希函数、哈希扩容、利用哈希分流找出大文件的重复内容、设计RandomPool结构、布隆过滤器、一致性哈希、并查集、岛问题
今天主要讨论:哈希函数.哈希表.布隆过滤器.一致性哈希.并查集的介绍和应用. 题目一 认识哈希函数和哈希表 1.输入无限大 2.输出有限的S集合 3.输入什么就输出什么 4.会发生哈希碰撞 5.会均匀 ...
- BloomFilter布隆过滤器
BloomFilter 简介 当一个元素被加入集合时,通过K个散列函数将这个元素映射成一个位数组中的K个点,把它们置为1.检索时,我们只要看看这些点是不是都是1就(大约)知道集合中有没有它了:如果这些 ...
- HBase之八--(3):Hbase 布隆过滤器BloomFilter介绍
布隆过滤器( Bloom filters) 数据块索引提供了一个有效的方法,在访问一个特定的行时用来查找应该读取的HFile的数据块.但是它的效用是有限的.HFile数据块的默认大小是64KB,这个大 ...
- BloomFilter布隆过滤器使用
从上一篇可以得知,BloomFilter的关键在于hash算法的设定和bit数组的大小确定,通过权衡得到一个错误概率可以接受的结果. 算法比较复杂,也不是我们研究的范畴,我们直接使用已有的实现. go ...
- 使用BloomFilter布隆过滤器解决缓存击穿、垃圾邮件识别、集合判重
Bloom Filter是一个占用空间很小.效率很高的随机数据结构,它由一个bit数组和一组Hash算法构成.可用于判断一个元素是否在一个集合中,查询效率很高(1-N,最优能逼近于1). 在很多场景下 ...
- SpringBoot(18)---通过Lua脚本批量插入数据到Redis布隆过滤器
通过Lua脚本批量插入数据到布隆过滤器 有关布隆过滤器的原理之前写过一篇博客: 算法(3)---布隆过滤器原理 在实际开发过程中经常会做的一步操作,就是判断当前的key是否存在. 那这篇博客主要分为三 ...
随机推荐
- 26、前端知识点--利用webpack搭建脚手架一套完整流程
前言 我们的目标是利用webpack搭建一个基于react + react-router +dva + es6 + less + antd用于中后台开发的脚手架,同学们可能会说社区里那么多优秀的脚手架 ...
- 237-基于Xilinx Kintex-7 XC7K325T 的FMC/千兆以太网/SATA/四路光纤数据转发卡
基于Xilinx Kintex-7 XC7K325T 的FMC/千兆以太网/SATA/四路光纤数据转发卡 一. 板卡概述 本板卡基于Xilinx公司的FPGAXC7K325T-2FFG900 芯片, ...
- 2、Jmeter测试
一.测试流程 1.添加本次测试计划 (右键-->添加-->Threads(Users)-->线程组) 2.设置线程数 (所谓线程数就是并发用户数) 3.在线程组内添加请求(右键--& ...
- P2617 Dynamic Rankings(待修改区间第k大)
题目链接:https://www.luogu.org/problemnew/show/P2617 题目: 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的 ...
- 一个奇怪的问题:Last_Errno: 1264 Error 'Out of range value for column 0x322E36343030
场景环境: 1. 主从都是:Server version: 5.7.16-log MySQL Community Server (GPL) 2.操作系统:CentOS release 6.7 (Fin ...
- 去除重复嵌套的html标签函数
去除重复嵌套的html标签 function strip_multi_tags($str, $tag = 'div'){ preg_match_all('/<'.$tag.'>|<\ ...
- root登录
,编辑/etc/lightdm/lightdm.conf: gedit /etc/lightdm/lightdm.conf [Seat:*] autologin-guest=false autolog ...
- FMDB复习
// colum/列/字段// row/行/记录// 主键的作用是唯一标识一条记录// sql语句注意:不区分大小写,以分号结束(不要分号也行?) // 如果增加字段,可能要指定数据类型,S ...
- c#代码规则,C#程序中元素的命名规范
俩种命名方法 1.Pascal 命名法,第一个字母大写其它字母小写Userid 2.Camel命名法,所有单第一方写大写,其它小写,骆峰命名法,userId C#程序中元素的命名规范项目名:公司名.项 ...
- Python3解leetcode First Bad Version
问题描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...