1

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?

4
  • 1
    When you have well tested implementation, why would you like to implement your own? Commented Oct 30, 2013 at 18:15
  • Its just about performance I am asking myself if I can improve it using primitive types Commented Oct 30, 2013 at 18:16
  • This would be extremely easy to implement both solutions and run a benchmark test comparing the speed between the two. Commented Oct 30, 2013 at 18:17
  • Please fix the 1st statement. Commented Oct 30, 2013 at 18:20

7 Answers 7

7

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.

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

6 Comments

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?
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.
@Nickolaus: The proper solution is to create a pre-sized ArrayList using the other constructor.
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
2. if I have a presized ArrayList the size still gets doubled and not expanded by a fixed value
|
2

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).

Comments

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.

Comments

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.

Comments

1

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.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.