The following programs perform random access and iteration of Java arrays, ArrayList, linkedlist, and vectors, and compare the performance of this collection.
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; }}//An anonymous class that performs an iterative operation statictest iteratetest = new Test ("iterate") {publicvoid test (L Ist<string> list) {for (inti=0; i<10; i++) {Iterato R<string>it = List.iterator (); while (It.hasnext ()) {it.next (); } } } }; Perform a random visitAsk the anonymous class statictest gettest = new Test ("get") {publicvoid test (list<string> List) { for (inti=0; I<list.size (); i++) {for (intk=0; k<10; k++) { List.get (k); } } } }; The anonymous class that performs the insertion 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"); } } }; The anonymous class that performs the deletion statictest removetest = new Test ("Remove") {publicvoid test (list<string> List) { Listiterator<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, G Ettest, removetest}; Test (tests,list); } staticpublic void Test (test[] tests, list<string> List) {for (inti=0; i<tests.length; i++) {System.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 = Nu ll Test array iteration and random access Operation 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); }}
Program Run Results
It can be seen from the results that the random access and iterative operation of the array is the fastest, the speed of inserting and deleting the LinkedList is the fastest, the speed of random access to the ArrayList is very fast, and the vector class has no outstanding performance in all aspects, and this class has not been advocated for use.
Performance comparisons for Java arrays and various lists