LinkedList vs. ArrayList performance comparison-single-thread comparison

Source: Internet
Author: User

To increase the persuasion, each operation can be set to repeat the number of repetitions, the greater the average result is more convincing. Here, 1 or 10 is set according to the situation (see the comment of the main function). There is time for students to set up a larger number.

Paste Results First:

The contrast is: add (); AddLast (); Get (); Remove () method.


Results Analysis:

Add method: The contrast performance is more obvious: the insertion time of linkedlist is significantly longer.

AddLast method: The difference is not small: from the end of the insertion to the LinkedList time may be shorter (cause to be analyzed).

Get method: LinkedList performance is much worse.

Remove method: LinkedList is significantly better than ArrayList.

The previous article has done a simple analysis from the source code. This article writes a single-threaded code comparison of both performance.

Post code:

Import Java.util.arraylist;import java.util.linkedlist;/** * Created by Aaashen on 2015/4/8.    */public class Vs {private ArrayList alist = new arraylist<integer> ();    Private LinkedList llist = new linkedlist<integer> ();    Private long starttime,starttime2 = 0;    Private long endtime,endtime2 = 0; Private Long Costtime,totalcosttime = 0;/** @param times* average number of times * @param num* operations to list * */private void Testarrayins        ERT (int times, int num) {long totalcosttime = 0;        Long costtime = 0;        Long startTime = 0;        Long endTime = 0;            for (int j=0; j<times; J + +) {alist = null;            Alist = new arraylist<integer> ();            Costtime = 0;            StartTime = 0;            EndTime = 0;            StartTime = System.currenttimemillis ();            for (int i = 0; i < num; i++) {alist.add (i);            } endTime = System.currenttimemillis ();   Costtime = Endtime-starttime;         Totalcosttime + = Costtime;    } System.out.println ("INSERT into ArrayList for" +times+ "Times Cost:" + totalcosttime/times);        } private void Testlinkedinsert (int times, int num) {long totalcosttime = 0;        Long costtime = 0;        Long startTime = 0;        Long endTime = 0;            for (int j=0; j<times; J + +) {llist = null;            Llist = new linkedlist<integer> ();            Costtime = 0;            StartTime = 0;            EndTime = 0;            StartTime = System.currenttimemillis ();            for (int i = 0; i < num; i++) {llist.add (i);            } endTime = System.currenttimemillis ();            Costtime = Endtime-starttime;        Totalcosttime + = Costtime;    } System.out.println ("INSERT into LinkedList for" +times+ "Times Cost:" + totalcosttime/times);        } private void Testarrayinsertlast (int times, int num) {long totalcosttime = 0; Long Costtime =0;        Long startTime = 0;        Long endTime = 0;            for (int j=0; j<times; J + +) {alist = null;            Alist = new arraylist<integer> ();            Costtime = 0;            StartTime = 0;            EndTime = 0;            StartTime = System.currenttimemillis ();            for (int i = 0; i < num; i++) {alist.add (i);            } endTime = System.currenttimemillis ();            Costtime = Endtime-starttime;        Totalcosttime + = Costtime;    } System.out.println ("INSERT into the last of ArrayList for" +times+ "Times Cost:" + totalcosttime/times);        } private void Testlinkedinsertlast (int times, int num) {long totalcosttime = 0;        Long costtime = 0;        Long startTime = 0;        Long endTime = 0;            for (int j=0; j<times; J + +) {llist = null;            Llist = new linkedlist<integer> ();            Costtime = 0;            StartTime = 0;   EndTime = 0;         StartTime = System.currenttimemillis ();            for (int i = 0; i < num; i++) {llist.addlast (i);            } endTime = System.currenttimemillis ();            Costtime = Endtime-starttime;        Totalcosttime + = Costtime;    } System.out.println ("INSERT into the last of LinkedList for" +times+ "Times Cost:" + totalcosttime/times);        } private void Testarrayfind (int times, int num) {long totalcosttime = 0;        Long costtime = 0;        Long startTime = 0;        Long endTime = 0;            for (int j=0; j<times; J + +) {alist = null;            Alist = new arraylist<integer> ();            Costtime = 0;            StartTime = 0;            EndTime = 0;            for (int i = 0; i < num; i++) {alist.add (i);            } startTime = System.currenttimemillis ();            for (int i = 0; i < num; i++) {alist.get (i); } endTime = SysTem.currenttimemillis ();            Costtime = Endtime-starttime;        Totalcosttime + = Costtime;    } System.out.println ("Find from ArrayList for" +times+ "Times Cost:" + totalcosttime/times);        } private void Testlinkedfind (int times, int num) {long totalcosttime = 0;        Long costtime = 0;        Long startTime = 0;        Long endTime = 0;            for (int j=0; j<times; J + +) {llist = null;            Llist = new linkedlist<integer> ();            Costtime = 0;            StartTime = 0;            EndTime = 0;            for (int i = 0; i < num; i++) {llist.add (i);            } startTime = System.currenttimemillis ();            for (int i = 0; i < num; i++) {llist.get (i);            } endTime = System.currenttimemillis ();            Costtime = Endtime-starttime;        Totalcosttime + = Costtime; } System.out.println ("Find from LinkedList for" +times+ "TiMes cost: "+ totalcosttime/times); }/* * @param times * avg. * @param num * Number of operations to list * @param num_remove * * */privat        e void Testarrayremove (int times, int num, int num_remove) {long totalcosttime = 0;        Long costtime = 0;        Long startTime = 0;        Long endTime = 0;            for (int j=0; j<times; J + +) {alist = null;            Alist = new arraylist<integer> ();            Costtime = 0;            StartTime = 0;            EndTime = 0;            for (int i = 0; i < num; i++) {alist.add (i);            } startTime = System.currenttimemillis ();            for (int i = 0; i < Num_remove; i++) {alist.remove (i);            } endTime = System.currenttimemillis ();            Costtime = Endtime-starttime;        Totalcosttime + = Costtime; } System.out.println ("Remove" +num_remove+ "numbers from ArrayList for" +times+ "Times Cost:" + TotAlcosttime/times);        } private void Testlinkedremove (int times, int num, int num_remove) {long totalcosttime = 0;        Long costtime = 0;        Long startTime = 0;        Long endTime = 0;            for (int j=0; j<times; J + +) {llist = null;            Llist = new linkedlist<integer> ();            Costtime = 0;            StartTime = 0;            EndTime = 0;            for (int i = 0; i < num; i++) {llist.add (i);            } startTime = System.currenttimemillis ();            for (int i = 0; i < Num_remove; i++) {llist.remove (i);            } endTime = System.currenttimemillis ();            Costtime = Endtime-starttime;        Totalcosttime + = Costtime; } System.out.println ("Remove" + Num_remove + "numbers from LinkedList for" +times+ "Times Cost:" + totalcosttime/    times);        } public static void Main (String args[]) {vs vs = new Vs (); Due to different length of operation, in order to reduce waiting time, the size is set for the same parameter. int times_small = 1; repetitions-Small int times_large = 10;//repetitions-large int num_small = 100000;//operation number-small int num_large = 1000000;//operation        -Large//insert operation time is short, so the set is a large parameter, compared to Representative Vs.testarrayinsert (Times_large,num_large);        Vs.testlinkedinsert (Times_large,num_large);        Vs.testarrayinsertlast (Times_large, Num_large);        Vs.testlinkedinsertlast (Times_large, Num_large);        LinkedList delete operation took too long time, so passed in a small parameter, does not affect the comparison vs.testarrayfind (Times_small, Num_small);        Vs.testlinkedfind (Times_small, Num_small);        The delete operation is set directly to 1000 delete vs.testarrayremove (Times_small, Num_small, 1000); Vs.testlinkedremove (Times_small, Num_small, 1000);}}









LinkedList vs. ArrayList performance comparison-single-thread comparison

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.