0

I want to know if there is a difference in performance if I use a primitive array and then rebuild it to add new elements like this:

AnyClass[] elements = new AnyClass[0];

public void addElement(AnyClass e) {
    AnyClass[] temp = new AnyClass[elements.length + 1];
    for (int i = 0; i < elements.length; i++) {
        temp[i] = elements[i];
    }
    temp[elements.length] = e;
    elements = temp;
}

or if I just use an ArrayList and add the elements.

I am not certain that is why I ask, is it the same speed because an ArrayList is build in the same way as I did it with the primitive array or is there really a difference and a primitive array is always faster even if I rebuild it everytime I add an element?

Nickolaus
  • 4,582
  • 4
  • 34
  • 58

7 Answers7

5

ArrayLists work in a similar way but instead of rebuilding every time they double there capacity every time the limit is reached. so if you are constantly adding to it ArrayLists will be faster because recreating the array is fairly slow. So your implementation could use less memory if you are not adding to it often but as far as speed goes it will be slower most of the time.

Todoy
  • 935
  • 8
  • 16
  • thanks your answer made it pretty clear why the ArrayList is the better solution (generally spoken), but if I know that my array will be in a range between a min and a max size could I improve the performance if I allocate a fixed number of elements (eg. 100) if the array is at its maximum size? – Nickolaus Oct 30 '13 at 18:23
  • If your range was extremely tight yes. but for a range of 100 no. With the new implementation you would have to resize 100 times, that would be very expensive. you can tell the ArrayList to start at a size of 100, so it would only have to resize once to get to a size of 200. – Todoy Oct 30 '13 at 18:27
  • 2
    @Nickolaus: The proper solution is to create a pre-sized `ArrayList` using the other constructor. – maaartinus Oct 30 '13 at 18:28
  • 1. the implementation above was only written by hand it is not in my code it should only be a quick example of what I mean – Nickolaus Oct 30 '13 at 18:29
  • 2. if I have a presized ArrayList the size still gets doubled and not expanded by a fixed value – Nickolaus Oct 30 '13 at 18:30
  • ArrayList has also an additional method trimToSize() to shink it. You can just dimension your ArrayList initially reasonably large (if you don't know the size in advance) and the strip it down to exact size after you added all the desired entries to it. – Durandal Oct 30 '13 at 18:37
2

When an ArrayList resizes it doubles itself, so that you are not wasting time resizing each time. Amortized, that means that it doesn't take any time to resize. That's why you shouldn't waste time recreating the wheel. The people who created the first one already learned how to make one more efficient and know more about the platform than you do.

Joel
  • 5,508
  • 1
  • 18
  • 18
1

In a nutshell, stick with ArrayList. It is:

  • widely understood;
  • well tested;
  • will probably be more performant that your own implementation (for example, ArrayList.add() is guaranteed to be amortised constant-time, which your method is not).
Community
  • 1
  • 1
NPE
  • 438,426
  • 93
  • 887
  • 970
1
  • There is no performance issue in both Arrays and ArrayList.
  • Arrays and ArrayList are index based so both will work in same way.
  • If you required the dynamic Array you can use arrayList.
  • If Array size is static then go with Array.
Prabhakaran Ramaswamy
  • 23,910
  • 10
  • 51
  • 62
0

Your implementation is likely to lose clearly to Java's ArrayList in terms of speed. One particularly expensive thing you're doing is reallocating the array every time you want to add elements, while Java's ArrayList tries to optimize by having some "buffer" before having to reallocate.

kviiri
  • 3,102
  • 1
  • 18
  • 30
0

ArrayList will also use internally Array Only , so this is true Array will be faster than ArrayList. While writing high performance code always use Array. For the same reason Array is back bone for most of the collections. You must go through JDK implementation of Collections. We use ArrayList when we are developing some application and we are not concerned about such minor performance issues and we do trade off because we get already written API to put , get , resize etc.

Vipin
  • 4,013
  • 3
  • 28
  • 52
0

Context is very important: I mean if you are constantly inserting new items/elements ArrayList will certainly be faster than Array. On the other hand if you just want to access an element at a known position-say arrayItems[8], Array is faster than ArrayList.get(8); Sine there is overhead of get() function calls and other steps and checks.

Kurt Van den Branden
  • 9,082
  • 8
  • 60
  • 72
AfamO
  • 501
  • 5
  • 5