Comparison of list efficiency in Java

Source: Internet
Author: User
Tags benchmark data structures java se intel core 2 duo

Inkfish original, do not reprint commercial nature, reproduced please indicate the source (http://blog.csdn.net/inkfish).

The Java Collections Framework (JCF) is a basic set of classes in Java SE, where almost all projects are used, and the list is one of the most commonly used interfaces in JCF. Around the list interface, there are many implementations, such as commonly used ArrayList, LinkedList, Vector, Stack, and Java5 after the introduction of Copyonwritearraylist, but also a number of list of open source implementation, such as Apache All kinds of list in commons-collections. (Source: Http://blog.csdn.net/inkfish)

So many list implementations, how to choose. How efficiently they are operating. This article will use specific code to detect some of the most commonly used list implementations. (Source: Http://blog.csdn.net/inkfish)

test Environment:
Processor: Intel Core 2 Duo P8600 2.4GHz
Memory: 2G
Hard drive: 160G 7200rpm
Java:sun JDK 1.6.0_15
Development environment: Eclipse 3.5
Third-party class library: Apache Commons-lang 2.4, Apache Commons-collections 3.2.1 (Source: http://blog.csdn.net/inkfish)

main test object:
Java.util.ArrayList;
Java.util.LinkedList;
Java.util.Stack;
Java.util.Vector;
Java.util.concurrent.CopyOnWriteArrayList;
Org.apache.commons.collections.FastArrayList;
Org.apache.commons.collections.list.TreeList; (Source: Http://blog.csdn.net/inkfish)

Test Cases:
1. Test List
1.1 Order Additions
1.2 Random Insertion
1.3 Random Deletion
1.4 Random Access
1.5 Random Updates
1.5 Sequential iterations
2. Test list in three kinds of circumstances of the sorting efficiency
2.1 The elements in the list are arranged in a small to large order (best case)
2.2 The elements in the list are sorted from large to small in the initial order (worst case)
2.3 Random arrangement of elements in the list at initial time, unordered
3. The efficiency of test list conversion
3.1 Conversion to Treelist
3.2 Conversion to ArrayList
3.3 Conversion to LinkedList
3.4 Conversion to Copyonwritearraylist
3.5 Conversion to Vector (source: http://blog.csdn.net/inkfish)

Test Code: (Source: http://blog.csdn.net/inkfish)

Package test; Import static java.lang.System.out; Import java.util.ArrayList; Import java.util.Collections; Import Java.util.Iterator; Import java.util.LinkedList; Import java.util.List; Import Java.util.Stack; Import Java.util.Vector; Import java.util.concurrent.CopyOnWriteArrayList; Import org.apache.commons.collections.FastArrayList; Import org.apache.commons.collections.list.TreeList; Import Org.apache.commons.lang.StringUtils; Import Org.apache.commons.lang.time.StopWatch; @SuppressWarnings ("Unchecked") public class Listperformance {public static void main (string[] args) {listperformance tes t = new Listperformance (10 * 10000); Out.print (Stringutils.center ("Test List performance:loop=" + test.loop, 80, '-')); out.printf ("/n%20s%10s%10s%10s%10s%10s%10s", "" "," Add "," Insert "," Remove "," get "," set "," iterator "); Test.benchmark (New Fastarraylist ()); Test.benchmark (New Treelist ()); Test.benchmark (New ArrayList ()); Test.benchmark (New LinkedList ()); Test.benchmark (New CopyonwritearrayList ()); Test.benchmark (New Vector ()); Test.benchmark (New Stack ()); 2. Test sort Out.print ("/n/n"); Out.print (Stringutils.center ("Test List sort performance:loop=" + test.loop, 80, '-')); out.printf ("/n%20s%10s%10s%10s", "" "," optimize "," worst "," random "); Test.benchmarksort (New Fastarraylist ()); Test.benchmarksort (New Treelist ()); Test.benchmarksort (New ArrayList ()); Test.benchmarksort (New LinkedList ()); Test.benchmarksort (New Copyonwritearraylist ());//unsupportedoperationexception Test.benchmarksort (New Vector ()) ; Test.benchmarksort (New Stack ()); 3. Test the transformation of Out.print ("/n/n") between various data structures; Out.print (Stringutils.center ("Test List convert performance:loop=" + test.loop, 80, '-')); out.printf ("/n%20s%10s%10s%10s%10s%10s", "" "," Tree "," Array "," linked "," Copyonwrite "," Vector "); Test.benchmarkconvert (New Fastarraylist ()); Test.benchmarkconvert (New Treelist ()); Test.benchmarkconvert (New ArrayList ()); Test.benchmarkconvert (New LinkedList ()); Test.benchmarkconvert (New Copyonwritearraylist ()); }/** TestTrial Cycle times * * private int loop = 10000; public listperformance (int loop) {this.loop = loop;} public void Benchmark (list list) {out.printf ("/n%20s", List.getcla SS (). Getsimplename ()); Int J; Stopwatch watch = null; 1. Test sequence Performance (ADD) (watch = new Stopwatch ()). Start (); for (int i = 0; I < loop; i++) {List.add (new Integer (i));} watch.stop (); out.printf ("%10d", Watch.gettime ()); 2. Test random Insert Performance (Random insert) (watch = new Stopwatch ()). Start (); for (int i = 0; I < loop; i++) {j = (int) (Math.random () * loop); List.add (J, New Integer (-J);} watch.stop (); out.printf ("%10d", Watch.gettime ()); 3. Test Random index deletion (Random remove) (watch = new Stopwatch ()). Start (); for (int i = 0; I < loop; i++) {j = (int) (Math.random () * loop); List.remove (j);} watch.stop (); out.printf ("%10d", Watch.gettime ()); 4. Test random Fetch performance (Random get) (watch = new Stopwatch ()). Start (); for (int i = 0; I < loop; i++) {j = (int) (Math.random () * loop); List.get (j);} watch.stop (); out.printf ("%10d", Watch.gettime ()); 5. Test random UpdatesPerformance (Random set) (watch = new Stopwatch ()). Start (); for (int i = 0; I < loop; i++) {j = (int) (Math.random () * loop); List.set (J, j);} watch.stop (); out.printf ("%10d", Watch.gettime ()); 6. Test iterative Performance (iterator) (watch = new Stopwatch ()). Start (); Iterator<object> iter = List.iterator (); while (Iter.hasnext ()) {Iter.next ();} watch.stop (); out.printf ("%10d", Watch.gettime ()); public void Benchmarkconvert (List list) {out.printf ("/n%20s", List.getclass (). Getsimplename ()); Stopwatch watch = null; 1. Turn treelist (watch = new Stopwatch ()). Start (); New Treelist (list); Watch.stop (); out.printf ("%10d", Watch.gettime ()); 2. Turn arraylist (watch = new Stopwatch ()). Start (); New ArrayList (list); Watch.stop (); out.printf ("%10d", Watch.gettime ()); 3. Turn linkedlist (watch = new Stopwatch ()). Start (); New LinkedList (list); Watch.stop (); out.printf ("%10d", Watch.gettime ()); 4. Turn copyonwritearraylist (watch = new Stopwatch ()). Start (); New Copyonwritearraylist (list); Watch.stop (); out.printf ("%10d", WATch.gettime ()); 5. Vector (watch = new Stopwatch ()). Start (); New Vector (list); Watch.stop (); out.printf ("%10d", Watch.gettime ()); public void Benchmarksort (List list) {out.printf ("/n%20s", List.getclass (). Getsimplename ()); Stopwatch watch = null; 1. Order list for (int i = 0; I < loop; i++) {List.add (new Integer (i));} (watch = new Stopwatch ()). Start (); Collections.sort (list); Watch.stop (); out.printf ("%10d", Watch.gettime ()); 2. Reverse list for (int i = loop-1 i > 0; i--) {List.add (new Integer (i));} (watch = new Stopwatch ()). Start (); Collections.sort (list); Watch.stop (); out.printf ("%10d", Watch.gettime ()); 3. Random order list for (int i = 0, j = 0; I < loop; i++) {j = (int) (Math.random () * loop); List.add (New Integer (j));} (watch = new Stopwatch ()). Start (); Collections.sort (list); Watch.stop (); out.printf ("%10d", Watch.gettime ()); } }

test Results: (Source: http://blog.csdn.net/inkfish)

-----------------------Test List performance:loop=100000-----------------------Add insert remove get Set iterator 59016 55391 copyonwritearraylist 54016 484003 370891 0 Vector 105406 8266 0 0 Stack 31 8281 8266 0 16 0- -------------------Test List sort performance:loop=100000---------------------optimize worst random fastarraylist 47 Treelist ArrayList LinkedList 109 Vector 0-M-m,-------------------Test L. ist convert performance:loop=100000-------------------tree Array linkedcopyonwrite Vector fastarraylist 0 0 0 0 0 Treeli St 0 0 0 0 0 ArrayList 0 0 0 0 0 linkedlist 0 0 0 0 0 copyonwritearraylist 0 0 0 0 0

Conclusion:
1. In random insertion, random deletion operation, the treelist efficiency is the highest;
2. In an environment where only append and iteration are required, linkedlist efficiency is highest;
3. Average efficiency, Arrayl Ist is relatively balanced, but if the massive random operation, still can cause the performance bottleneck;
4.CopyOnWriteArrayList because of thread safety, the performance is much lower, so use caution;
5.Vector is less efficient than legendary;
6. Let S Tack to do list things, but semantic stack shouldn't do too many list things;
7. In the sort, ArrayList has the best performance, the Treelist average performance is also good, linkedlist The sorting efficiency is greatly influenced by the initial state of the element.
8. There is almost no time loss between the various list conversions. (Source: Http://blog.csdn.net/inkfish)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.