Array vs ArrayList in performance

125,044

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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

Share:
125,044
Spark-Beginner
Author by

Spark-Beginner

I appreciate all edits to my answers, and promptly approve most edit suggestions.

Updated on July 05, 2022

Comments

  • Spark-Beginner
    Spark-Beginner almost 2 years

    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?

  • SnakeDoc
    SnakeDoc over 10 years
    because it's backed by an underlying array. therefore, anything wrapping an array cannot be faster than the array.
  • assylias
    assylias over 10 years
    Resizing is a moot argument - you can pre allocate an arraylist the same way you have to pre allocate an array.
  • Ted Hopp
    Ted Hopp over 10 years
    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.
  • Rahul Tripathi
    Rahul Tripathi over 10 years
    @TedHopp:- Yes you are right Sir. That was misleading! Removed that part! :)
  • Christian Kuetbach
    Christian Kuetbach over 10 years
    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.
  • TwoThe
    TwoThe over 10 years
    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.
  • andrea.rinaldi
    andrea.rinaldi over 8 years
    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 :)
  • Admin
    Admin over 6 years
    @TedHopp Can you provide more details/links explaining why List#get() is slower than ArrayList#get()
  • Ted Hopp
    Ted Hopp over 6 years
    @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.