1000万数据对比ContainsAll实测
数据 对比 1000 实测
2023-06-13 09:15:00 时间
JDK版本java11
public void timeoutReminder() {
List<String> list = new ArrayList<>();
List<String> list2 = new ArrayList<>();
for (int i = 0; i < 10000000; i++) {
Random random = new Random();
list.add(getRandomString(random.nextInt(100)));
list2.add(getRandomString(random.nextInt(100)));
}
StopWatch stopWatch = new StopWatch();
stopWatch.start();
list.containsAll(list2);
stopWatch.stop();
System.out.println(stopWatch.getTotalTimeSeconds());
StopWatch stopWatch2 = new StopWatch();
stopWatch2.start();
CollectionUtils.containsAll(list, list2);
stopWatch2.stop();
System.out.println(stopWatch2.getTotalTimeSeconds());
}
0.571
17.27
源码 java.util.List#containsAll
public boolean containsAll(Collection<?> c) {
Object[] es = getArray();
int len = es.length;
for (Object e : c) {
if (indexOfRange(e, es, 0, len) < 0)
return false;
}
return true;
}
private static int indexOfRange(Object o, Object[] es, int from, int to) {
if (o == null) {
for (int i = from; i < to; i++)
if (es[i] == null)
return i;
} else {
for (int i = from; i < to; i++)
if (o.equals(es[i]))
return i;
}
return -1;
}
从源码中看到在循环内还有list.len次循环,时间复杂度为0(N2);
.collections4.CollectionUtils#containsAll
而在CollectionUtils中除了用set去重之外,还在遍历前中分别加入了过滤条件
/**
* Returns <code>true</code> iff all elements of {@code coll2} are also contained
* in {@code coll1}. The cardinality of values in {@code coll2} is not taken into account,
* which is the same behavior as {@link Collection#containsAll(Collection)}.
* <p>
* In other words, this method returns <code>true</code> iff the
* {@link #intersection} of <i>coll1</i> and <i>coll2</i> has the same cardinality as
* the set of unique values from {@code coll2}. In case {@code coll2} is empty, {@code true}
* will be returned.
* <p>
* This method is intended as a replacement for {@link Collection#containsAll(Collection)}
* with a guaranteed runtime complexity of {@code O(n + m)}. Depending on the type of
* {@link Collection} provided, this method will be much faster than calling
* {@link Collection#containsAll(Collection)} instead, though this will come at the
* cost of an additional space complexity O(n).
*
* @param coll1 the first collection, must not be null
* @param coll2 the second collection, must not be null
* @return <code>true</code> iff the intersection of the collections has the same cardinality
* as the set of unique elements from the second collection
* @since 4.0
*/
public static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2) {
if (coll2.isEmpty()) {
return true;
} else {
final Iterator<?> it = coll1.iterator();
final Set<Object> elementsAlreadySeen = new HashSet<Object>();
for (final Object nextElement : coll2) {
if (elementsAlreadySeen.contains(nextElement)) {
continue;
}
boolean foundCurrentElement = false;
while (it.hasNext()) {
final Object p = it.next();
elementsAlreadySeen.add(p);
if (nextElement == null ? p == null : nextElement.equals(p)) {
foundCurrentElement = true;
break;
}
}
if (foundCurrentElement) {
continue;
} else {
return false;
}
}
return true;
}
}
理论上在处理数据时应该是CollectionUtils的containsAll方法个更快的,但是实测的简单非对象存储数据随机数,反而list.containsAll更快,实际场景还是要实际分析的
相关文章
- 存储和操作n维数据的难题,谷歌用一个开源软件库解决了
- 主流大数据平台及解决方案对比「建议收藏」
- 大数据ClickHouse(二十):ClickHouse 可视化工具操作
- 云计算与大数据技术应用 第四章课后答案_云计算原理与实践
- origin怎么做多组柱状图_origin怎么对比两组数据
- 对比Hadoop和 Spark,看大数据框架进化之路
- DES数据加密标准
- 【Android RTMP】Android Camera 视频数据采集预览 ( NV21 图像格式 | I420 图像格式 | NV21 与 I420 格式对比 | NV21 转 I420 算法 )
- 【Groovy】自定义 Xml 生成器 BuilderSupport ( 构造 Xml 节点类 | 封装节点名称、节点值、节点属性、子节点 | 将封装的节点数据转为 Xml 字符串 )
- SQL SERVER ANALYSIS SERVICES决策树、聚类、关联规则挖掘分析电商购物网站的用户行为数据
- Oracle批量插入数据的三种方式介绍及对比
- MongoDB使用mongoexport和mongoimport命令,批量导出和导入JSON数据到同一张表的实例
- SQL数据分表Mybatis Plus动态表名优方案
- Redis(十二):redis两种持久化方法对比分析详解大数据
- MySQL中使用求差函数解决数据差异问题(mysql求差函数)
- 对比Oracle中表数据的差异(oracle比较表数据)
- 热备VCS及Oracle双机热备保障数据可靠性(vcsoracle双机)
- Oracle数据库对比工具:快速对比、精准查找(oracle数据对比工具)
- 微软推出Microsoft Edge for Linux新版本 终于支持同步书签和密码等数据
- 对比MySQL中的二进制对比分析(mysql二进制数据)
- 探索MongoDB:存储海量数据的最佳选择(mongodb大数据存储)
- MySQL中的Credit 帮助你管理信用数据的完美解决方案(mysql中credit)
- 深度解析如何进行 MySQL 两表数据对比(mysql两表数据对比)
- MySQL数据对比比较两行值(mysql 两行值比较)
- MySQL实现两行值对比 可以用SELECT语句和JOIN语句完成数据比较并输出不同的字段值
- C语言与Oracle行数据的功能性对比(c oracle行数据)
- Oracle事务处理维护数据一致性(oracle 事物处理)
- Riak与Redis强大的分布式数据存储对比分析(riak redis比较)
- 分析oracle中两列数据的对比差异(oracle 两列比较)
- 比较Oracle数据库中两表数据对比分析(oracle 两个表数据)
- mssql30万条数据搜索文本字段的各种方式对比
- perl实现的两个文件对比并对数据进行筛选的脚本代码
- MySQL查询随机数据的4种方法和性能对比