Java Object List Performance Analysis

  For the comprehensive analysis of the performance differences between the categories, we must know their method.    Therefore, I would like the following from the point of view of performance, a brief introduction of the realization of these characteristics. 

  First, the realization of Vector and ArrayList 
  Vector and ArrayList are with a bottom of the Object [] array, the Object [] array elements to preserve.    Index visit through the elements, you simply visit indexed by internal array elements: 
  Public Object get (int index) 
  (/ / Check first whether legitimate index … here do not show this part of the code return 
  ElementData [index];) 

  Internal arrays can be more than Vector / ArrayList object with the number of elements, both as a residual of the margin space, in order to achieve quickly add new elements.    With the remaining space, adding elements of a very simple, the only new element to the preservation of an internal array of spare position, and then a new location on the margins of increase Index Value: 
  Public boolean add (Object o) 
  (EnsureCapacity (size + 1) / / later on elementData [size + +] = o; return true; 
  / / List.add (Object) return value) 

  Set in the arbitrary elements inserted in the position specified by (rather than at the end of the pool) a little bit more complex: insertion point on the array elements must all move forward one position, and then are assigned to: 
  Public void add (int index, Object element) ( 
  / / First check whether legitimate index … here do not show this part of the code 
  EnsureCapacity (size +1); 
  System.arraycopy (elementData, index, elementData, index + 1, 
  Size - index); 
  ElementData [index] = element; 
  Size + +; 
  ) 

  The remaining space was used up, and if necessary to add more elements Vector / ArrayList object must be an even greater array of new replacement of its internal Object [] array, all the elements of an array copied to the new array.    According to different versions of the SDK, the new array than the original 100% or 50% (the code shown below the expanding array of 100%): 
  Public void ensureCapacity (int minCapacity) ( 
  Int oldCapacity = elementData.length; 
  If (minCapacity> oldCapacity) ( 
  Object oldData [] = elementData; 
  Int newCapacity = Math.max (oldCapacity * 2, minCapacity); 
  ElementData = new Object [newCapacity]; 
  System.arraycopy (oldData, 0, elementData, 0, size); 
  ) 
  ) 

  Vector ArrayList class and the principal difference is synchronization.    In addition to the two serial only for the method, not a ArrayList synchronous implementation of the method is the ability to the contrary, the majority of Vector method has synchronization capabilities, either directly or indirectly.    Therefore, Vector is thread-safe, but not ArrayList.    This makes ArrayList than Vector fast.    For some of the latest JVM, two of the differences in speed can be ignored Excluding: Strictly speaking, these JVM, these two categories the difference in speed is less than the comparative performance of these tests showed that the time difference. 

  Index to access and update through the elements, ArrayList Vector and the realization of a superior performance, as there are no checks in addition to the scope of the other expenses.    Unless an array of internal expansion space must be exhausted, otherwise add to the list at the end of the element, or deleted from the list at the end of the element, it has equally outstanding performance.    Insert and delete elements to the array element has to be copied (when the array must first expand, it needs to reproduce twice).    Be copied and the number of elements [size-index] proportional, that is, and insert / delete points in the final index to set the distance between the location of proportion.    The insert operation, the elements inserted into the set top (indexing 0), the worst performance, inserted into the final set when the plane (after the end of an existing element), the best performance.    With the increasing scale of collection, array copying expenses also increasing rapidly, because each operation must insert copy of the increase in the number of the element. 

  Second, the realization of LinkedList 
  LinkedList through a two-way link list of nodes to achieve.    Index visit to pass elements, you have to find all the nodes, until a target node: 
  Public Object get (intindex) ( 
  / / First check whether legitimate index … here do not show this part of the code 
  Entry e = header; / / start node 
  / / Find forward or backward, from which a specific direction than the distance 
  / / Close decision 
  If (index <size / 2) ( 
  For (int i = 0; i <= index; i + +) 
  E = e.next; 
  Else () 
  For (int i = size; i> index; i -) 
  E = e.previous; 
  ) 
  Return e; 
  ) 

  List of the insert elements are very simple: find a designated index nodes, the nodes before the close and then insert a new node: 
  Public void add (int index, Object element) ( 
  / / First check whether legitimate index … here do not show this part of the code 
  Entry e = header; / / starting node 
  / / Find forward or backward, from which a specific direction than the distance 
  / / Close decision 
  If (index <size / 2) ( 
  For (int i = 0; i <= index; i + +) 
  E = e.next; 
  Else () 
  For (int i = size; i> index; i -) 
  E = e.previous; 
  ) 
  Entry newEntry = new Entry (element, e, e.previous); 
  NewEntry.previous.next = newEntry; 
  NewEntry.next.previous = newEntry; 
  Size + +; 
  ) 

  Thread safety LinkedList if from the pool and other Java SDK with a thread-safe, LinkedList, you can use a package of sync from Collections.synchronizedList (List) with a.    However, the use of synchronous accession package for the equivalent of an indirect layer, it will bring high performance cost.    When the package calls for the transfer to the packaging method, a method is the need to increase an additional Method Invocation, after simultaneous package Package approach than the method without a package two to three times slower.    As the search for such complex operations, such indirect costs brought about by the call is not very prominent, but the relatively simple methods, such as access function or update function, the performance of such expenses may have serious repercussions. 

  This means that, and Vector compared to the synchronous package LinkedList performance in a significant disadvantage because Vector thread-safe and do not need to carry out any additional indirect call.    If you want to have a thread-safe, LinkedList, you can copy the LinkedList several categories and the necessary means for synchronization, so you can get a faster implementation.    For all other collections, which are equally effective: Only List and Map is an efficient realization of the security thread (a Vector and Hashtable).    Interestingly, these two categories efficient thread-safe only to the existence of backward compatibility, and is not based on performance considerations. 

  The visit by indexing and updating of elements LinkedList achieve the performance overhead slightly, because an index visit arbitrary demand across multiple nodes.    Insert element across multiple nodes in addition to the performance overhead, but also other expenses, which targets the creation node costs.    In the edge, LinkedList achieve insertion and deletion of no other operating expenses, therefore, insert - removing overhead almost entirely dependent on Insert - deleted from the collection points at the end of the distance. 

  Reply from: http://www.javaresearch.org/article/showarticle.jsp?column=545&thread=51136 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags: ,

Releated Articles


0 Comments to “Java Object List Performance Analysis”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.