76

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?

7
  • Why is this important? What problem are you actually trying to solve? Commented Oct 15, 2013 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. Commented Oct 15, 2013 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. Commented Oct 15, 2013 at 19:41
  • 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. Commented Oct 15, 2013 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 : pastebin.com/Gw4T1u9v Commented Oct 15, 2013 at 20:10

5 Answers 5

85

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 :)
39

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.

6 Comments

Resizing is a moot argument - you can pre allocate an arraylist the same way you have to pre allocate an array.
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.
@TedHopp:- Yes you are right Sir. That was misleading! Removed that part! :)
@TedHopp Can you provide more details/links explaining why List#get() is slower than ArrayList#get()
@naaz - It's mostly true when there is no JIT compiler at runtime. See this thread 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.
|
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.

Comments

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.

Comments

10

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

2 Comments

because it's backed by an underlying array. therefore, anything wrapping an array cannot be faster than the array.
I don't think that the performance will be different: docjar.com/html/api/java/util/ArrayList.java.html The only overhead is a rangeCheck.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.