项目实战之1000万数据对比ContainsAll实测

简介: 1000万数据对比ContainsAll实测

欢迎点击主页查看更多内容....

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;
    }
源码org.apache.commons.collections4.CollectionUtils#containsAll

 /**
     * 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;
        }
    }
目录
相关文章
|
1月前
|
监控 前端开发 关系型数据库
常见性能工具一览
今天写了一个调试工具的文章,就有人说起工具到底要会哪些。既然提到这儿了,那就多写几句吧。
47 2
常见性能工具一览
|
8月前
|
新能源
会后分享 | 精选十二:整车性能开发解决方案
本文由上海安世亚太公司汽车行业技术经理章敏先生在新能源汽车研讨会上所发表的演讲,此内容详细地讲解了整车性能开发解决方案。
会后分享 | 精选十二:整车性能开发解决方案
|
4月前
|
消息中间件 缓存 监控
阿里P8整理的《百亿级并发系统设计》实战教程,实在是太香了
说实话,如果面试官问你这个题目,那么你必须要使出全身吃奶劲了。为啥?因为你没看到现在很多公司招聘的 JD 里都是说啥有高并发经验者优先。
|
4月前
|
JSON 测试技术 API
【测试平台系列】第一章 手撸压力机(十一)-初步实现性能测试
上一章节我们组合了场景,它是一个list结构。今天我们实现性能测试计划的数据结构及其方法.
|
6月前
|
Cloud Native 物联网 Linux
万字解读|怎样激活 TDengine 最高性价比?
经过我们不断地打磨优化之后,TDengine 3.0 在性能、功能、稳定性各个方面均有大幅提升,已经从一款时序数据库蜕变成为高性能、云原生、分布式的物联网、工业大数据平台。
100 0
|
7月前
|
设计模式 运维 Java
硬核!阿里P8耗时3月撰写700页性能优化笔记:程序优化提升了7倍
前言 在我看来,Java性能优化是Java进阶的必经之路,性能优化作为Java工程师必备的一种技术,一直热度不减。 Java是目前软件开发领域中使用最广泛的编程语言之一。Java应用程序在许多垂直领域(银行、电信、医疗保健等)中都有广泛使用。帮助开发者通过专注于JVM内部,性能调整原则和最佳实践,以及利用现有监测和故障诊断工具,来提升应用程序在商业环境中的性能。
|
7月前
|
消息中间件 设计模式 缓存
阿里2023年版十亿级并发系统设计+java性能优化实战文档
2023年注定是不平凡的一年,这一年充满机遇和挑战,就看你是否能够抓住这个稍纵即逝的机会。 说快也快,说慢也慢,这不马上就金九银十了,你是否已经准备好升职加薪或者跳槽涨薪了呢? 没准备好也别怕,小编给大家准备好了2023年最新版阿里巴巴十亿级并发系统设计+阿里巴巴java性能调优实战两份学习文档,让你面试阿里不再难,希望大家能够喜欢!
第55/90步《后端篇》第2章 优化游戏体验与性能 第4课
今天学习《后端篇》第2章 优化游戏体验与性能 第4课 优化游戏体验:添加背景图片和顶层UI
45 0
|
存储 Java 数据库
第56/90步《后端篇》第2章 优化游戏体验与性能 第5课
今天学习《后端篇》第2章 优化游戏体验与性能 第5课 优化游戏性能:监听全局错误,记录错误日志
55 0
|
SQL 分布式计算 大数据
七、【计算】Presto架构原理与优化介绍(上) | 青训营笔记
七、【计算】Presto架构原理与优化介绍(上) | 青训营笔记
七、【计算】Presto架构原理与优化介绍(上) | 青训营笔记

热门文章

最新文章