Compare the performance summary of Java arrays and various list _java

Source: Internet
Author: User

Don't talk much, just look at the sample code

Package cn.lion.test;
   public class Performancetest {privatestatic final int SIZE = 100000;
     Publicstatic Abstract class test{privatestring operation;
     Publictest (String operation) {this.operation= operation;
     } publicabstract void Test (list<string> List);
     Publicstring getoperation () {returnoperation;
       The anonymous class statictest iteratetest = new Test ("iterate") {publicvoid Test (list<string> List) {
          For (inti=0 i<10; i++) {iterator<string>it = List.iterator ();
          while (It.hasnext ()) {it.next ();
   }
       }
     }
   }; Anonymous class Statictest gettest = new test (' get ') {publicvoid test (list<string> List) {for (inti=0; i&) performing random access Lt;list.size ();
          i++) {for (intk=0; k<10; k++) {list.get (k);
   }
       }
     }
   }; Anonymous class Statictest inserttest = new Test (' Insert ') {publicvoid test (list<string>List) {listiterator<string>it = List.listiterator (List.size ()/2);
       For (inti=0 i<size; i++) {It.add ("lion");
   }
     }
   }; Anonymous class Statictest removetest = new Test ("remove") {publicvoid test (list<string> List) {Listiterat
       Or<string>it = List.listiterator ();
          while (It.hasnext ()) {it.next ();
       It.remove ();
   }
     }
   };
     staticpublic void Testarray (list<string> List) {test[]tests = {iteratetest, gettest};
   Test (tests,list); } staticpublic void Testlist (list<string> List) {test[]tests = {inserttest, iteratetest, Gettest, removeTest
     };
   Test (tests,list); } staticpublic void Test (test[] tests, list<string> List) {for (inti=0; i<tests.length; i++) {Syste
       M.out.print (tests[i].getoperation () + "Operation:");
       LONGT1 = System.currenttimemillis ();
       Tests[i].test (list);
       LONGT2 = System.currenttimemillis (); System.ouT.print (t2-t1+ "MS");
     System.out.println ();
     } publicstatic void Main (string[] args) {list<string>list = null;
     Test array iterations and random access Operations System.out.println ("------Test array------");
     STRING[]TSTR = new String[size];
     Arrays.fill (Tstr, "lion");
     list= arrays.aslist (TSTR);
     
     Testarray (list);
     Tstr= New STRING[SIZE/2];
     
     Collection<string>coll = Arrays.aslist (TSTR);
     Test vector System.out.println ("------test vector------");
     list= new vector<string> ();
     List.addall (coll);
     
     Testlist (list);
     Test LinkedList System.out.println ("------Test linkedlist------");
     list= new linkedlist<string> ();
     List.addall (coll);
     
     Testlist (list);
     Test ArrayList System.out.println ("------test vector------");
     list= new arraylist<string> ();
     List.addall (coll);
   Testlist (list); }
}

Run the results as shown

As can be seen from the result, the speed of random access and iterative operation of an array is the fastest, and the insertion and deletion of LinkedList is the fastest, and random access to ArrayList is fast; the vector class has no outstanding performance in all respects, and this class is no longer advocated for use.

Summarize

The above is the entire content of this article, I hope to learn from you or use Java can help. If you have questions, you can leave a message for discussion.

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.