05.25.08
Java Performace tips and tricks
# ArrayList is faster than Vector except when there is no lock acquisition required in HotSpot JVMs (when they have about the same performance).
# Vector and ArrayList implementations have excellent performance for indexed access and update of elements, since there is no overhead beyond range checking.
# Adding elements to, or deleting elements from the end of a Vector or ArrayList also gives excellent performance except when the capacity is exhausted and the internal array has to be expanded.
# Inserting and deleting elements to Vectors and ArrayLists always require an array copy (two copies when the internal array must be grown first). The number of elements to be copied is proportional to [size-index], i.e. to the distance between the insertion/deletion index and the last index in the collection. The array copying overhead grows significantly as the size of the collection increases, because the number of elements that need to be copied with each insertion increases.
# For insertions to Vectors and ArrayLists, inserting to the front of the collection (index 0) gives the worst performance, inserting at the end of the collection (after the last element) gives the best performance.
# LinkedLists have a performance overhead for indexed access and update of elements, since access to any index requires you to traverse multiple nodes.
# LinkedList insertions/deletion overhead is dependent on the how far away the insertion/deletion index is from the closer end of the collection.
# Synchronized wrappers (obtained from Collections.synchronizedList(List)) add a level of indirection which can have a high performance cost.
# Only List and Map have efficient thread-safe implementations: the Vector and Hashtable classes respectively.
# List insertion speed is critically dependent on the size of the collection and the position where the element is to be inserted.
# For small collections ArrayList and LinkedList are close in performance, though ArrayList is generally the faster of the two. Precise speed comparisons depend on the JVM and the index where the object is being added.
# Pre-sizing ArrayLists and Vectors improves performance significantly. LinkedLists cannot be pre-sized.