17

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.

Popcorn
  • 5,188
  • 12
  • 54
  • 87
  • 2
    If you can guarantee that, you could go ahead with an array for performance reasons. I would however, only do that where you know that it is a performance bottleneck, since that makes your code less flexible. Premature optimization is the root of all evil. – Ruan Mendes Jul 23 '12 at 23:02

8 Answers8

29

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?
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • 2
    Performance is never negligible. I'm paid by the cycle =P – arkon May 11 '13 at 05:46
  • @b1naryatr0phy Can you explain more on the cycle part ? – Peter Jan 12 '14 at 08:53
  • @Peter http://en.wikipedia.org/wiki/Cycles_per_instruction – arkon Jan 13 '14 at 03:42
  • 1
    @Peter: When you say a CPU is 2GHz, that means it can perform 2 billion mathematical operations per second--or, in other words, the CPU "cycles" every two-billionth of a second. b1narytr0phy was joking that he got paid based on how many of these "cycles" his code required. – StriplingWarrior Jan 13 '14 at 16:03
7

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.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
4

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;
lessthanoptimal
  • 2,722
  • 2
  • 23
  • 25
2

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.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
1

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.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
0

You shouldn't optimize your code too early, instead use the data structure which fits in your code best. as you said a rule of thumb could be:

  1. i don't know the number of elements or it is changing a lot => List (don't fixate on implementations)

  2. the number of elements is fixed an known beforehand => array

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
Absurd-Mind
  • 7,884
  • 5
  • 35
  • 47
  • I don't think the premature optimization rule of thumb applies to API-changing decisions – Idan Arye Jul 23 '12 at 23:08
  • @aetheria The rule applies well on the bodies of black-box functions, where the optimization does not impact other parts of the program, so you can only benefit from optimizing while having a working program you can profile. In API's, while you can still benefit from that profiling data, the optimization **can** impact other parts of the program. For example, if he has a function that returns an array\`ArrayList`, and he wants to switch, he has to change code that uses that function, and if that code returns that array\`ArrayList`, he has to change code that uses that code and so on. – Idan Arye Jul 24 '12 at 00:00
  • I disagree; compromising the usability of your API for the purposes of optimization is not something to be done without substantial profiling data and a strong business case. Even then, there are almost certainly better ways to tackle the situation - e.g. design the API in a "tell, don't ask" style so that such implementation decisions are more hidden. That's not to say that arrays aren't easy to use. If they improve the API's usability, that's fine. If you're using them purely for optimization, and making the API harder to use as a result, in my view, that's a bad move. – ᴇʟᴇvᴀтᴇ Jul 24 '12 at 06:35
  • @aetheria Most decent languages allow hiding the implementation details - which is the better choice. But we are not talking about decent languages - we are talking about Java here. In Java, arrays don't share interfaces with the more sophisticated collections(like in C#), generics are not strong enough to handle both arrays and `ArrayList`(like in C++), and you can't use type inference to hide those details away(like in D). Java doesn't even have `typedef` or `alias` to mask the type you actually use. – Idan Arye Jul 24 '12 at 10:46
  • I don't want to get into a language war. Java allows you to hide technical details. It also allows you to reveal them. What I'm saying is that when you develop an API, it's better not to reveal implementation details. You definitely don't want to do it without a good reason. "I think it will be faster" is not a good reason. – ᴇʟᴇvᴀтᴇ Jul 24 '12 at 12:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14360/discussion-between-idan-arye-and-aetheria) – Idan Arye Jul 24 '12 at 19:25
0

if you know the size of the array, ensuring that it wont expand or shrink. than use arrays. if you dont know, array list are preferable and stable. for performance, is this your only concern left ? move along..

DarthVader
  • 52,984
  • 76
  • 209
  • 300
-2

Arrays much faster than array list. The difference can be hundreds of percents and probably come from the JVM optimization. If you declare your array variables as volatile than you will get similar performance.

vadim
  • 9