Array or List in Java. Which Is Faster

Array or List in Java. Which is faster?

I suggest that you use a profiler to test which is faster.

My personal opinion is that you should use Lists.

I work on a large codebase and a previous group of developers used arrays everywhere. It made the code very inflexible. After changing large chunks of it to Lists we noticed no difference in speed.

are arrays faster than arraylist?

Any performance difference will be negligible, especially if you initialize your arraylist with an initialCapacity. Write your code in whatever way makes it the most readable and maintainable, and try not to optimize stuff like this unless you've determined through testing that you are getting a significant performance hit from it.

Potential reasons to use ArrayList:

  • useful methods (contains, etc.)
  • implements Iterable, Collection, and List, and can therefore be used in a lot of API calls that are interface-based.
  • even though you currently "know" that your collection's size can never change, life throws unexpected requirements at us. Why lock yourself into a pattern when there's no discernible advantage to be gained?

Array vs ArrayList in performance

It is pretty obvious that array[10] is faster than array.get(10), as the later internally does the same call, but adds the overhead for the function call plus additional checks.

Modern JITs however will optimize this to a degree, that you rarely have to worry about this, unless you have a very performance critical application and this has been measured to be your bottleneck.

Array to List or List to Array, which conversion is faster?

Arrays.asList is faster because it does not copy data - it returns an object wrapping the array passed to it, and implementing the List interface. Collection.toArray() copies the data to the array, so it runs in O(N) instead of O(1).

Java Performance - ArrayLists versus Arrays for lots of fast reads

Now that you've mentioned that your arrays are actually arrays of primitive types, consider using the collection-of-primitive-type classes in the Trove library.

@viking reports significant (ten-fold!) speedup using Trove in his application - see comments. The flip-side is that Trove collection types are not type compatible with Java's standard collection APIs. So Trove (or similar libraries) won't be the answer in all cases.

Are arrays supposed to be faster than ArrayLists by this much?

Use Integer a, which save one unboxing and one boxing operation.

    for (int i = list.size()-1; i>0; i--){
int index=rnd.nextInt(i+1);
Integer a=list.get(index);
list.set(index,list.get(i));
list.set(i,a);
}

And the Integer objects use more memory.


@Andy Turner mentioned the exist Collections#swap.

    for (int i = list.size()-1; i > 0; i--) {
int index = rnd.nextInt(i+1);
Collections.swap(list, i, index);
}

Without warm-up of JIT compiler this might slow-down the bench-mark,
but will look better in production code. Though then you would probably use the Collections.shuffle anyway.


As commented the swap version is fast too. First the OP showed using the right microbenchmarking code.

swap uses the original Integer class too. It does l.set(i, l.set(j, l.get(i))); in order to swap - as set returns the previous element at that position. The JIT compiler can probably unwrap set and utilize that previous element immediately.

What is faster: ArrayList of int [] or int [][]

[Assuming you meant ArrayList<> rather than Array<>, since the latter is impossible].

By all technical measures, a native array will always be faster and smaller than an ArrayList<>, although the difference may in many cases be very minimal. Internally an ArrayList uses a native array to store its elements, so it cannot possibly outperform that array.

However the ArrayList<E> has the advantage that it implements all of the List<E> interface as well as Collection<E> and Iterable<E>. This can make your code far simpler by allowing you to directly pass the object to any function that will accept parameters that implement those interfaces.

A further advantage of ArrayList<E> is that the array can transparently grow in size so it may be preferred if you do not know in advance how large the array has to be. However that itself comes at a performance cost - if the new size is larger than the current capacity of the backing store array then a whole new array must be created, and then the original elements all copied into it - an O(n) operation.

What works faster: two dimensional arrays or list of lists

The array will almost certainly be faster.

Using an ArrayList will bring the performance more in-line since it's backed by an actual array.

Edit to summarize comments

  • Lists are re-sizeable. May or may not be an issue.
  • Performance differences tend towards minimal.
  • It should be benchmarked to know for sure.

For this usecase I believe the arrays will be measurably faster. Whether it's faster enough to matter is a different issue, and I don't know enough about the actual problem being solved to make a judgement on that.



Related Topics



Leave a reply



Submit