75

Which one is better in performance between Array of type Object and ArrayList of type Object?

Assume we have a Array of Animal objects : Animal animal[] and a arraylist : ArrayList list<Animal>

Now I am doing animal[10] and list.get(10) which one should be faster and why?

Java Devil
  • 10,629
  • 7
  • 33
  • 48
Spark-Beginner
  • 1,334
  • 5
  • 17
  • 24
  • Why is this important? What problem are you actually trying to solve? – Ted Hopp Oct 15 '13 at 19:40
  • If you're only doing that a few times, whichever one comes to mind or has the fewest keystrokes will be fastest. You'll have to elaborate on this question. – clwhisk Oct 15 '13 at 19:41
  • 1
    ArrayLists are backed by Arrays (hence the name). There would be essentially zero discernible speed difference under almost all circumstances. In any case, there's a trivial way to check: *try it*. – Dave Newton Oct 15 '13 at 19:41
  • possible duplicate of [Array or List in Java. Which is faster?](http://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster) - benchmark: http://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster/16565376#16565376 – assylias Oct 15 '13 at 19:55
  • 1
    For simple accesses and set operations the `[]` array will outperform the List by roughly a factor of 2-4. But that's multiplying times a very small amount of time. But if you ever need to resize the array or do something else more complex than simple set/access the List form will be quite a bit more convenient and likely a bit better performer. – Hot Licks Oct 15 '13 at 20:02
  • 2
    @Kayaman and at Ted Hopp - here's your test. 300% faster to use Array's in this very very basic example. You can see, it will extrapolate if the array/list is larger or has more complex operations : http://pastebin.com/Gw4T1u9v – SnakeDoc Oct 15 '13 at 20:10
  • @SnakeDoc your test is flawed on two counts: the array uses primitives so the list bears the additional cost of boxing and you don't warmup the JVM properly... I would not trust the result too much. – assylias Oct 15 '13 at 20:55

5 Answers5

82

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.

TwoThe
  • 13,879
  • 6
  • 30
  • 54
  • 13
    The only thing that you should worry about is the dynamic allocation in heap memory that an array list performs when it reaces the pre-allocated memory: in this cases JITs can do nothing. There are several ways to avoid this problem (like instantiating the array list with a defined length) but many developers seem to not care about it. Anyway, good answer :) – andrea.rinaldi Sep 11 '15 at 15:56
38

From here:

ArrayList is internally backed by Array in Java, any resize operation in ArrayList will slow down performance as it involves creating new Array and copying content from old array to new array.


In terms of performance Array and ArrayList provides similar performance in terms of constant time for adding or getting element if you know index. Though automatic resize of ArrayList may slow down insertion a bit Both Array and ArrayList is core concept of Java and any serious Java programmer must be familiar with these differences between Array and ArrayList or in more general Array vs List.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 14
    Resizing is a moot argument - you can pre allocate an arraylist the same way you have to pre allocate an array. – assylias Oct 15 '13 at 19:57
  • 2
    The benchmark in the second link is terrible. It access the array list through an interface declaration (`List.get())`, which is known to be slower than a class access (`ArrayList.get()`). There's no warm-up phase. The arrays are large, so paging issues are mixed in with the timing results. – Ted Hopp Oct 15 '13 at 20:01
  • 1
    @TedHopp:- Yes you are right Sir. That was misleading! Removed that part! :) – Rahul Tripathi Oct 15 '13 at 20:03
  • The second quote does only talk about the theoretical performance of get operations, it doesn't say that they are actually the same speed. This is misleading. – TwoThe Oct 15 '13 at 21:00
  • 1
    @TedHopp Can you provide more details/links explaining why List#get() is slower than ArrayList#get() –  Jan 20 '18 at 10:05
  • 1
    @naaz - It's mostly true when there is no JIT compiler at runtime. See [this thread](https://stackoverflow.com/questions/6839943/why-are-interface-method-invocations-slower-than-concrete-invocations) for more information. In most scenarios the difference is essentially zero, but in a benchmark attempt like this, the effect is perhaps not so easily ignored, particularly when the JIT compiler (if any) is not given an opportunity to warm up. – Ted Hopp Jan 20 '18 at 23:13
21

When deciding to use Array or ArrayList, your first instinct really shouldn't be worrying about performance, though they do perform differently. You first concern should be whether or not you know the size of the Array before hand. If you don't, naturally you would go with an array list, just for functionality.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
11

I agree with somebody's recently deleted post that the differences in performance are so small that, with very very few exceptions, (he got dinged for saying never) you should not make your design decision based upon that.

In your example, where the elements are Objects, the performance difference should be minimal.

If you are dealing with a large number of primitives, an array will offer significantly better performance, both in memory and time.

user949300
  • 15,364
  • 7
  • 35
  • 66
10

Arrays are better in performance. ArrayList provides additional functionality such as "remove" at the cost of performance.

Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
  • 9
    because it's backed by an underlying array. therefore, anything wrapping an array cannot be faster than the array. – SnakeDoc Oct 15 '13 at 19:43
  • 1
    I don't think that the performance will be different: http://docjar.com/html/api/java/util/ArrayList.java.html The only overhead is a rangeCheck. – Christian Kuetbach Oct 15 '13 at 20:40