Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are arrays faster than arraylist? [duplicate]

My intuition says arrays are faster than arraylist because arraylists are implemented using arrays that resize as it fills up/loses elements.

I just wanted to confirm if this is true or not, implying there's never a reason to use an arraylist if you know the number of elements you want to hold.

like image 434
Popcorn Avatar asked Jul 23 '12 22:07

Popcorn


5 Answers

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?
like image 188
StriplingWarrior Avatar answered Oct 04 '22 15:10

StriplingWarrior


ArrayList gives you many features a raw array does not have. If you know the number of elements you can create an ArrayList of that size.

new ArrayList<String>(100);

If you are worrying about the difference in speed between an ArrayList and an array, you are worrying about the wrong thing. It is highly unlikely to be the bottleneck in your code. If it is, there is almost certainly a better answer than changing to an array.

Don't succumb to premature optimization. It'll wreak havoc on your code. Most things don't matter, only a few things do. You can only find those few things by profiling your code. Trying to make every part fast is a very ineffective way of making the whole fast. Keeping a clean, simple design is much more effective. That will give you the necessary seams for introducing optimizations in the one or two places they're actually needed.

like image 44
ᴇʟᴇvᴀтᴇ Avatar answered Oct 04 '22 14:10

ᴇʟᴇvᴀтᴇ


As other people have said already, use ArrayList unless performance benchmarks say it is a bottle beck.

Yes there is a performance difference due to accessor overhead, which will not be significant in general. An exception to that general rule is if you are storing primitive types inside your ArrayList. That will encore a significant performance hit as it converts the objects to/from Object and primitive types.

// easier to work with but slow
ArrayList<Double> slowList;

// faster primitive data array
double[] fasterList;
like image 37
lessthanoptimal Avatar answered Oct 04 '22 16:10

lessthanoptimal


ArrayList can't be faster at simple get/set, but the difference would be very small and likely not worth worrying about in almost any realistic scenario.

The List API has some methods you may need, even in the case you already know the size will be fixed. Think contains() for example. You'd also have to use ArrayList in a case where you wanted an Iterator or an API needed a List -- again even if you know the list is of fixed size.

like image 34
Sean Owen Avatar answered Oct 04 '22 16:10

Sean Owen


Yes, arrays are much faster. You can see a noticeable speed-up if you running numerical algorithms.

However for normal application purposes, you don't have to worry about it. This is because most of the runtime is spent doing other things anyways.

like image 1
tskuzzy Avatar answered Oct 04 '22 16:10

tskuzzy