1

Possible Duplicate:
Array or List in Java. Which is faster?

Is it much more expensive to use ArrayList than a simple array in Java? I have lots of ArrayList and in some places I can use array, I am not sure if I should go through the trouble of replacing them all.

Concretely, I can imagine get for ArrayList is more expensive than using the operator [] for arrays.

Thanks

Community
  • 1
  • 1
user1796942
  • 2,542
  • 4
  • 25
  • 36
  • 2
    Is `ArrayList` manipulation the performance bottleneck of your application? If not, I wouldn't worry about it. –  Jan 06 '13 at 02:13
  • And those ArrayLists are filled with primitive types or objects created by you? I would keep ArrayList... – Survivor Jan 06 '13 at 02:13
  • 1
    `get()` is O(1) for `ArrayList`, see http://stackoverflow.com/questions/2182597/time-complexity-for-java-arraylist – us2012 Jan 06 '13 at 02:17

4 Answers4

3

It's a teeny bit more expensive. If your data isn't fixed size, and you do anything other than iterating over it, it's worth using ArrayList for the added readability and simplicity.

The one exception is for primitive types, for which primitive arrays are more likely to win.

Louis Wasserman
  • 172,699
  • 23
  • 307
  • 375
2

Donald Knuth made the following two statements on optimization:

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil" (From Wikipedia)

In other words, unless you have some benchmarks that indicate that you need to use a Java array then use the one that benefits you as a developer; which, in my opinion, is the ArrayList.

GSP
  • 3,550
  • 2
  • 31
  • 51
2

Generally speaking, if you need to do stuff that's really time-sensitive, the use of ArrayList versus Object[] is not going to be where your bottleneck is. Database queries, network communication, disk access, and un-optimized arrays and such are going to be far worse.

Since ArrayList is not synchronized, the performance is generally not going to be substantially worse than Object[], particularly if you specify the size before adding elements.

Colselaw
  • 1,019
  • 9
  • 19
2

A very high percentage of the cost of an application is its design, development and maintenance. By comparison, the cost of running the application is usually so much smaller it is not worth worrying about. So when you think about efficiency, you should think about your efficiency and those who have to maintain your application after you (assuming your application will be useful ;)

Say you have application where performance is an issue. A common mistake is to just guess where the performance issue are and "optimise" those. The problem with this approach is that it usually

  • adds complexity
  • reduces maintainability.
  • misses the biggest performance bottlenecks
  • can make little or no difference or can even make performance worse. (The last time some one tried to optimise some code I had written it was 50% slower ;)

What you need to do is to perform some realistic tests and measure the performance bottlenecks which exceed your specific measurable business requirements A vague go-as-fast-as-possible is not specific or measurable, and usually non-sense in my experience.

Once you have made the changes to the key bottlenecks you need to re-test the system to ensure it really helped.

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075