简体   繁体   English

对性能CollectionUtils.isEmpty()或collection.isEmpty()有什么好处

[英]What is better for the performance CollectionUtils.isEmpty() or collection.isEmpty()

What is better for the performance if you already know that the collection isn't null. 如果您已经知道集合不为null,那么对性能有什么好处。 Using !collection.isEmpty() or CollectionUtils.isNotEmpty(collection) from the Apache Commons lib? 使用Apache Commons lib中的!collection.isEmpty()CollectionUtils.isNotEmpty(collection)

Or isn't there any performance difference? 或者没有任何性能差异?

The code of CollectionUtils.isNotEmpty (assuming we are talking about Apache Commons here)... CollectionUtils.isNotEmpty的代码(假设我们在这里讨论Apache Commons)...

public static boolean isEmpty(Collection coll)
{
    return ((coll == null) || (coll.isEmpty()));
}

public static boolean isNotEmpty(Collection coll)
{
    return (!(isEmpty(coll)));
}

...so, not really a difference, that one null check will not be your bottleneck ;-) ...所以,不是真正的区别,一个空检查不会是你的瓶颈;-)

The other answers are correct, but just to be sure about it: 其他答案是正确的,但只是为了确定它:

Why do you care at all? 你为什么要关心? Does your application have a performance problem; 您的应用程序是否存在性能问题; and careful profiling pointed to that method; 并仔细分析指出该方法; so you are looking for alternatives? 所以你在寻找替代品?

Because ... if not ... then it could be that we are looking at PrematureOptimization . 因为......如果不是......那么我们可能会看到PrematureOptimization

And one other aspect: if "java standard libraries" provide a feature; 另一方面:如果“java标准库”提供一个功能; I would always prefer them over something coming from an "external library". 我总是喜欢他们来自“外部图书馆”的东西。 Of course, ApacheCommons is quite commons nowadays; 当然,ApacheCommons现在很公共; but I would only add the dependency towards it ... if all my other code is already using it. 但我只会添加依赖...如果我所有其他代码已经使用它。

The difference is negligible (extra null check), all calls can be easily inlined even by C1 compiler. 差异可以忽略不计(额外的空检查),所有调用都可以通过C1编译器轻松内联。 In general you should not worry about performance of such simple methods. 一般来说,您不应该担心这些简单方法的性能。 Even if one of them is twice slower it's still blazingly fast compared to the rest code of your application. 即使其中一个慢两倍,它仍然比你的应用程序的其余代码快得多。

Collection.isEmpty as CollectionUtils which is defined in apache libs is indirectly going to use the collection.isEmpty() method. Collection.isEmpty作为在apache libs中定义的CollectionUtils间接使用collection.isEmpty()方法。

Although no noticable difference present in both of them, It's just that 虽然两者都没有明显的差异,但就是这样

CollectionUtils.isEmpty is NullSafe and as you say that you know that collection is not empty , so Both are equally Good (Almost) CollectionUtils.isEmptyNullSafe ,正如你所说,你知道集合不是空的,所以两者都同样好(几乎)

With the following program you can see the clear results with 1000 Integer in the List. 使用以下程序,您可以在列表中看到1000 Integer的清晰结果。 Note: time is in milliseconds 注意:时间以毫秒为单位

collection.isEmpty is almost 0 milliseconds collection.isEmpty几乎是0毫秒

CollectionsUtils.isNotEmpty take 78 milliseconds CollectionsUtils.isNotEmpty需要78毫秒

public static void main(String[] args){
            List<Integer> list = new ArrayList<Integer>();
            for(int i = 0; i<1000; i++)
                list.add(i);

            long startTime = System.currentTimeMillis();
                list.isEmpty();
            long endTime   = System.currentTimeMillis();
            long totalTime = endTime - startTime;
            System.out.println(totalTime);


            long startTime2 = System.currentTimeMillis();
            CollectionUtils.isNotEmpty(list);
            long endTime2   = System.currentTimeMillis();
            long totalTime2 = endTime2 - startTime2;
            System.out.println(totalTime2);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM