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.