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());
}
}
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());
}
}
相关文章推荐
- Arraylist和linkedlist的性能测试
- ArrayList和LinkedList性能测试
- ArrayList和LinkedList区别及性能测试
- ArrayList和LinkedList性能对比测试
- Java容器(二)——「ArrayList、LinkedList性能测试与分析」
- 关于List接口中ArrayList,LinkedList和Vector的取值方法的一些性能测试
- 测试java的ArrayList与LinkedList的性能
- paip.提升性能---list,arraylist,vector,linkedlist,map的选用..
- ArrayList和LinkedList的各项操作性能比较
- LinkedList, ArrayList等使用场景和性能分析)
- ArrayList和LinkedList的各项操作性能比较
- Java Collections Framework - Java集合框架之List篇 ArrayList与LinkedList等的性能比较
- java性能优化-Arraylist与Linkedlist整改查性能比较
- 测试ArrayList与LinkedList追加数据时间长短
- 数组、ArrayList、LinkedList查询及遍历性能分析 .
- JAVA LinkedList和ArrayList的使用及性能分析
- Java性能优化-Arraylist与Linkedlist整改查性能比较的简介与内容
- JAVA [ 性能优化 ArrayList LinkedList]
- Java使用Arrays、ArrayList、LinkedList、Vector实现插入查询性能分析
- ArrayList及LinkedList遍历方式及性能比较