您的位置:首页 > 其它

ArrayList 和 LinkedList 性能测试

2017-02-27 11:43 393 查看
import java.util.ArrayList;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.List;

import java.util.ListIterator;

public class ListPerformance{
private static final int PERS = 100;
private abstract static class Tester{
String name;
int size;
Tester(String name, int size){
this.name = name;
this.size = size;
}
abstract void test(List a);
}
private static Tester[] tests = {
new Tester("get", 300){
void test(List a){
for(int i=0; i<PERS; i++){
for(int j=0; j<a.size(); j++){
a.get(j);
}
}
}
},
new Tester("iteration", 300){
void test(List a){
for(int i=0; i<PERS; i++){
Iterator it = a.iterator();
while(it.hasNext()){
it.next();
}
}
}
},
new Tester("insert", 1000){
void test(List a){
ListIterator it = a.listIterator(3);
while(it.hasNext()){
it.next();
it.remove();
}
}
},

};
public static void test(List a){
System.out.println("Testing " + a.getClass().getName());
for(int i=0; i<tests.length; i++){
Collection1.fill(a, tests[i].size);
System.out.println(tests[i].name);
long t1 = System.currentTimeMillis();
tests[i].test(a);
long t2 = System.currentTimeMillis();
System.out.println(" : " + (t2 - t1));
}
}
public static void main(String[] args) {
test(new ArrayList());
test(new LinkedList());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: