Java Object List of performance analysis and testing
SDK provides a set order to achieve several interface java.util.List, three of which most people are familiar with the Vector, ArrayList and LinkedList. List of these is a difference between the performance of frequently asked questions. In this article, I want to explore is LinkedList and Vector / performance difference between the ArrayList.
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:
(/ / 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:
(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:
/ / 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%):
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.
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:
/ / 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:
/ / 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 + +;
)
LinkedList security threads and other pool
If Java SDK should be 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.
There are many different categories of these functions can be tested. LinkedList application more frequently, because people think it in the random insert and delete operations with better performance. Therefore, I am now focused on the analysis of the operation will be inserted into the performance, that is, construction sets. I tested and compared the LinkedList and ArrayList, as both are non-synchronous.
Insert the speed of operation from the main pool size and the location of elements inserted decision. When inserted in the location of the point and the two ends of the middle set, the worst performance and inserted into the best performance of all opportunities that arise. Therefore, I chose the three insert location (set at the beginning, middle and end), a collection of three typical Size: Medium (100 elements), and large (10,000 elements), the super-large (1 million elements).
In this test, I am using a JAVA SDK 1.2.0 and 1.3.0 series SUN JVM. In addition, I also used HOTSPOT JVM 2.0 to the test, version 1.3.0 SDK can be found. In the table below, all measured by the time one of the SDK 1.2 VM test time (in the form shown as 100% of the unit) for the baseline display. Test the use of the default JVM configuration, that is, the opening of the JIT compiler, for all JVM, heap space must be extended in order to avoid memory overflow error. Form record time is the average time-tested. In order to avoid the impact of refuse collection in various tests conducted between I fully compulsory liquidation of memory (see test source code for details). Pagination disk monitoring to ensure that the disk will not arise in the course of testing (any test, if it shows serious disk paging operation, were discarded). All showed a few seconds of the slow response time of the test is repeated until a clear record to a reasonable period of time.
For smaller sets, ArrayList, and the performance is very close to LinkedList. When the elements inserted into the pool at the end, that is an additional element, ArrayList in the performance of the mutation. However, additional elements are ArrayList optimized for a particular operation: If you only want a fixed size of the static pool, Java arrays (such as Object []) than any set targets have better performance. Apart from the additional operations, measured the time difference is not much data, they reflect the various JVM optimization level, and not something else.
For example, the set of elements inserted into the starting position, (Table 1 of the first two lines), plus LinkedList HotSpot JVM 2.0 is the best performance (85.3%), in the second position is 1.2 JVM increase ArrayList (100% ). These two results show that, in simple 1.2 JIT compiler in the implementation of iterative and reproduction, such as an array of simple operation with high efficiency. In HotSpot JVM with the complexities of the compiler optimization to improve the performance of complex operations, such as object creation (the creation LinkedList nodes), and be able to use embedded code (code-inlining) advantage. 1.3 JVM to the results seem to indicate that, in its simple operation and great performance deficiencies, it was possible in future versions of the JVM be improved.
Here I am particularly testing is ArrayList compared to the LinkedList Another advantage of pre-determined that the ability to set the size. Specifically, the creation of ArrayList time allows you to specify a specific size (for example, in testing for ArrayList can create with the capacity of 100 elements), so as to avoid all elements with the increasing size of the increase in collection costs. Table 1 shows that the number in parentheses set of pre-set size of the extent of the increase. LinkedList (until SDK 1.3) can not be pre-determined size.
In addition, ArrayList generate only a small amount of the need for refuse collection target, namely, to preserve the element of the array of internal targets, as well as each ArrayList inadequate capacity expansion, creating the need for additional internal array object. LinkedList regardless of any potential delete operation, for each of the insert operation generates a node object. Therefore, the LinkedList garbage collectors will bring a lot of work. Taking these factors into account, for any small and medium-sized pool, I would choose not to use ArrayList LinkedList.
Table 2 shows that a large-scale set of test results. We can see that in the event of a large-scale operation into the time, we began to experience severe performance penalty. As we have in front of the realization of the results obtained, the LinkedList, in the worst case in the elements when inserted into the middle set. In addition, we can also see, and when to use ArrayList elements inserted into the pool compared to the beginning of the worst properties, the use of the elements LinkedList when inserted into the middle of the performance pool some even worse. And the two worst performance compared to the situation, the elements inserted into the middle of ArrayList properties obviously much better.
Overall view, ArrayList Once again, in most cases showed better performance, including the index of the random elements inserted into the location of the situation. If you always been trying to insert elements set forward in the location of LinkedList has better performance, however, you can use a reverse ArrayList better performance, namely, the realization of the use of a dedicated, or through [size-index] mapping overturned in the index set in place.
Table 3 shows the large set of test results can be derived from the table in Table 2 and the conclusions are very similar. However, Table 3 stressed that the requirements of large data sets, set type, data-processing algorithm between the right support, otherwise, you will be in fact unacceptable performance. As for performance optimization, you can construct a special address the problem of collections. The large collection, in order to obtain acceptable performance, structural special collections often is necessary.
In the category of internal enquiries for achieving maximum performance. For enquiries these lists, all the elements iteration of the time required is a limiting factor. ArrayList / Vector category will be achieved on the elements of iteration. The following example of the total number of empty elements:
For (int i = 0; i <size; i + +)
If (elementData [i] == null)
Count + +; LinkedList class will search for the realization of all the nodes. All the following examples of the total number of empty elements:
Node = header.next;
Count = 0;
For (int i = 0; i <repeat; i + +, node = node.next)
If (node.element == null)
Count + +;
Table 4 shows that ArrayList performance significantly exceeded the LinkedList, it once again shows ArrayList should be our preferred category. Table 5 shows the use of the List.listIterator (int), and was the object iteration ListIterator all the elements of the time required, if not in the List for internal mechanisms to achieve these iterators is essential. ArrayList once again showed a higher performance, but this is not the extent of the difference, as shown in table 4, as inconceivable. Note that Table 5 shows the absolute time equivalent Table 4 shows that the absolute time of 10 times, that is, traversing some internal ArrayList than ArrayList use ListIterator 10 times faster iteration.
The actual measurement and we have to consider other factors have clearly shows that ArrayList and Vector usually higher than LinkedList and synchronization package after the LinkedList a better performance. Even if you think that may provide better performance LinkedList the circumstances, you may amend by adding elements from ArrayList the way for better performance, for example, reversed the order of the element in the collection.
LinkedList there will be some cases better performance, for example, when large number of elements need to join the large pool at the beginning and at the end. But generally speaking, I suggest you use priority ArrayList / Vector category, only when they exist obvious performance problems and LinkedList to improve their performance when they use LinkedList.
Tags: object, performance








0 Comments to “Java Object List of performance analysis and testing”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.