Why is list.size()>0
slower than list.isEmpty()
in Java? On other words why isEmpty()
is preferable over size()>0
?<
For ArrayList
, yes — you are correct that the operations take (roughly) the same time.
For other implementations of List
— for example, a naïve linked list* — counting the size might take a very long time, while you only actually care whether it is greater than zero.
So if you absolutely know that the list is an implementation of ArrayList
and will never ever change, then it does not really matter; but:
size() == 0
is still not faster than isEmpty()
, so there is no compelling reason to ever use the former.isEmpty()
is a clearer definition of what it is you actually care about and are testing, and so makes your code a bit more easily understandable.* I originally wrote LinkedList here, implicitly referencing java.util.LinkedList
, though that particular implementation does store its size explicitly, making size()
an O(1) operation here. A naïve linked list operation might not do this, and in the more general sense there is no efficiency guarantee on implementations of List
.
Your testing code is flawed.
Just reverse the order, i.e call isEmpty first and size > 0 second and you'll get the opposite result. This is due to class loading, caching, etc.
It is impossible to say in general which is faster, because it depends on which implementation of interface List
you are using.
Suppose we're talking about ArrayList
. Lookup the source code of ArrayList
, you can find it in the file src.zip in your JDK installation directory. The source code of the methods isEmpty
and size
looks as follows (Sun JDK 1.6 update 16 for Windows):
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
You can easily see that both expressions isEmpty()
and size() == 0
will come down to exactly the same statements, so one is certainly not faster than the other.
If you're interested in how it works for other implementations of interface List
, look up the source code yourself and find out.
According to PMD ( static ruleset based Java source code analyzer ) isEmpty() is preferred. You can find the PMD ruleset here. Search for "UseCollectionIsEmpty" rule.
http://pmd.sourceforge.net/rules/design.html
According to me it also helps in keeping the entire source code consistent rather than half of the folks using isEmpty() and the rest using size()==0.
I'm sorry, but your benchmark is flawed. Take a look at Java theory and practice: Anatomy of a flawed microbenchmark for a general description on how to approach benchmarks.
Update: for a proper benchmark you should look into Japex.
Given those two implementations, the speed should be the same, that much is true.
But those are by far not the only possible implementations for these methods. A primitive linked list (one that doesn't store the size separately) for example could answer isEmpty()
much faster than a size()
call.
More importantly: isEmpty()
describes your intent exactly, while size()==0
is unnecessarily complex (not hugely complex of course, but any unnecessary complexity at all should be avoided).