• Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance again

 
vaibhav jain
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody make it clear that which approach is better ..creating Object[] or ArrayList to put my objects.
 
Peter Reinhardt
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally if you don't really have a performance problem, just use the one which is more convenient (Arraylist can grow dynamically).
If you care about performance, an Arraylist is internally built as an object-array (Object[]). The only difference is, that an ArrayList grows dynamically where if you instantiates an array the full array is initialized (with null - pointers obviously).
So if you keep adding items to your ArrayList the system has to make the internal array bigger and copy all the old items to the new array.
Peter.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vaibhav jain:
Can anybody make it clear that which approach is better ..creating Object[] or ArrayList to put my objects.

depends on what you do with it after you create it. If you are using it for temporary storage, then sequential retrieval, then the Object[] might suit you the best. If you are doing any searching, sorting, adding, deleting then one of the collection classes would be best ( depending on the use, ArrayList may or may not be the best choice, but has good overall performance as a generic collection choice ). To see which collection class to use read through the very helpful tutorial
here
 
Peter Reinhardt
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think searching or sorting are any different they are implemented the same (Arrays.binarySearch() and Collections.binarySearch() ). Adding and deleting is slower for an arraylist (but more comfortable).
Peter
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Reinhardt:
I don't think searching or sorting are any different they are implemented the same (Arrays.binarySearch() and Collections.binarySearch() ). Adding and deleting is slower for an arraylist (but more comfortable).
Peter
Although the implementations may be the same, the performances differ depending on the type of data structure used. So the use of the Collection object creates a performance difference. This was taken from JavaTM 2 Platform, Standard Edition, v1.2.2 API Specification and shows how the basic operations performance differ:
TreeMap:
This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Corman, Leiserson, and Rivest's Introduction to Algorithms.
HashMap:
This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets.
HashSet:
This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets.
TreeSet:
This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).
ArrayList:
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking).
As for the sorting, I more or less meant that if you need to maintain a certain order within your elements, using a SortedSet or SortedMap may simplify things or reduce the number of calls to Collections.sort().
By the way, I couldn't find performance values for Sorted collections. They are probably faster in searches and slower in additions/deletions though.
Jamie
[ August 08, 2002: Message edited by: Jamie Robertson ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I couldn't find performance values for Sorted collections.
SortedSet and SortedMap are just interfaces - performance depends on a particular implementation. Try TreeSet:
This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

They are probably faster in searches and slower in additions/deletions though.
Well, to use binarySearch() the collection or array needs to be sorted, or it won't work. And for the contains() method, TreeSet is log(n) while ArrayList, etc. are linear.
And don't overlook LinkedList, which is great if you have a lot of inserts or deletes anywhere other than the end of the list. But for random access (get(int), binarySearch(), sort()) it sucks. (Note precise use of mathematical language.)
Vaibhav - if you're not sure, using an ArrayList and declaring it as a Collection or List wherever possible is probably easiest. This makes it easier to switch to another collection type later if needed. I'd only use arrays if speed is important in this part of the code, and either (a) I know that I won't have to resize the array after it's created, or (b) I'm willing to write extra code to resize the array as necessary.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!