Arrays.asList () performance. Contains () versus multiple if equal to instructions

Which of these instructions is better in terms of performance and memory usage:

if(val.equals(CONSTANT1) || val.equals(CONSTANT2) ..... || val.equals(CONSTANTn)) {

}

      

OR

if(Arrays.asList(CONSTANT1,CONSTANT2, ..... ,CONSTANTn).contains(val)) {

}

      

+3


source to share


4 answers


Better to ask how to write this code more clearly (and faster if performance really matters). The answer to that would be an expression switch

(or maybe even polymorphism if you want to convert your constants to an enum) or a lookup array.

But if you insist on comparing your two matches, the former is slightly faster. To see this, let's see what the second aspect entails:

  • create a new array with constants to pass to the vararg parameter in Arrays.asList
  • create a new list object wrapping this array
  • iterating over this array, comparing each element with equal


The third step is equivalent to your first approach.

Finally, it's worth noting that such an operation will likely take much less than a microsecond, so unless you call this method millions of times per second, either approach is fast enough.

+1


source


Theoretically # 1 is faster, but not significantly, because Arrays.asList creates only one object - a list view (wrapper) of the specified array, there is no copying of the array:



public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}

private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
    private static final long serialVersionUID = -2764017481108945198L;
private final E[] a;

ArrayList(E[] array) {
        if (array==null)
            throw new NullPointerException();
    a = array;
}

      

+2


source


Since you are not using a loop, I am assuming that the number of values ​​is so small that in practice any differences would be irrelevant.

However, having said that if someone has to iterate manually and use equals () versus asList () and contains () ... it would still be the same.

Arrays.asList () returns a private implementation of a list that extends AbstractList and simply wraps around the existing array by reference (no copy is made). The contains () method uses indexOf (), which iterates through the array, using equals () on each element, until it finds a match, and then returns it. If you break your loop when you find equal, then both implementations will be quite equivalent.

The only difference would be the tiny memory size for the extra list structure generated by Arrays.asList (), besides this ...

+1


source


if (val.equals (CONSTANT1) || val.equals (CONSTANT2) ..... || val.equals (CONSTANTn)) {

}

is the best in terms of performance and memory, because the second one will take time to create a list and start searching for val in that list. This requires additional memory to maintain the list, as well as additional time spent iterating over the list. If comparing val to a constant would use a short-circuited comparison approach.

0


source







All Articles