测试java的ArrayList与LinkedList的性能
2015-04-13 19:02
330 查看
二把刀真是误事,我对数据结构了解不多,在用ArrayList与LinkedList的时候我曾经倾向用LinkedList,以为链表就方便插入,后来做一个实验。
代码:
public class ListEfficiencyTest {
public static void main(String[] args) {
int count = 10000;
long begin1 = System.nanoTime();
ArrayList<String> arraylist = new ArrayList<String>();
for(int i=0;i<count;i++){
arraylist.add("test");
}
long end1 = System.nanoTime();
System.out.println("ArrayList add "+count+" String, costs time "+(end1-begin1));
begin1 = System.nanoTime();
for(int i=0;i<arraylist.size();i++){
arraylist.get(i);
}
end1 = System.nanoTime();
System.out.println("ArrayList get(index) "+count+" String, costs time "+(end1-begin1));
begin1 = System.nanoTime();
Iterator<String> it1 = arraylist.iterator();
while(it1.hasNext()){
it1.next();
}
end1 = System.nanoTime();
System.out.println("ArrayList iterator "+count+" String, costs time "+(end1-begin1));
long begin2 = System.nanoTime();
LinkedList<String> linkedlist = new LinkedList<String>();
for(int i=0;i<count;i++){
linkedlist.add("test");
}
long end2 = System.nanoTime();
System.out.println("LinkedList add "+count+" String, costs time "+(end2-begin2));
begin2 = System.nanoTime();
for(int i=0;i<linkedlist.size();i++){
linkedlist.get(i);
}
end2 = System.nanoTime();
System.out.println("LinkedList get(index) "+count+" String, costs time "+(end2-begin2));
begin2 = System.nanoTime();
Iterator<String> it = linkedlist.iterator();
while(it.hasNext()){
it.next();
}
end2 = System.nanoTime();
System.out.println("LinkedList iterator "+count+" String, costs time "+(end2-begin2));
}
}
用相同量的数据测试ArrayList与LinkedList的插入耗时、索引取值、Iterator迭代取值的耗时,结果如下:
耗时均为毫微秒数:
现象:
1、同样多的数据,LinkedList的耗时反倒更大。
2、进行index索引遍历时,数据量越大,LinkedList的效率就越更低。
3、在使用Iterator进行遍历集合内容时,LinkedList的的效率更优,但是优势不明显。
结论:
以后尽量用ArrayList了。
代码:
public class ListEfficiencyTest {
public static void main(String[] args) {
int count = 10000;
long begin1 = System.nanoTime();
ArrayList<String> arraylist = new ArrayList<String>();
for(int i=0;i<count;i++){
arraylist.add("test");
}
long end1 = System.nanoTime();
System.out.println("ArrayList add "+count+" String, costs time "+(end1-begin1));
begin1 = System.nanoTime();
for(int i=0;i<arraylist.size();i++){
arraylist.get(i);
}
end1 = System.nanoTime();
System.out.println("ArrayList get(index) "+count+" String, costs time "+(end1-begin1));
begin1 = System.nanoTime();
Iterator<String> it1 = arraylist.iterator();
while(it1.hasNext()){
it1.next();
}
end1 = System.nanoTime();
System.out.println("ArrayList iterator "+count+" String, costs time "+(end1-begin1));
long begin2 = System.nanoTime();
LinkedList<String> linkedlist = new LinkedList<String>();
for(int i=0;i<count;i++){
linkedlist.add("test");
}
long end2 = System.nanoTime();
System.out.println("LinkedList add "+count+" String, costs time "+(end2-begin2));
begin2 = System.nanoTime();
for(int i=0;i<linkedlist.size();i++){
linkedlist.get(i);
}
end2 = System.nanoTime();
System.out.println("LinkedList get(index) "+count+" String, costs time "+(end2-begin2));
begin2 = System.nanoTime();
Iterator<String> it = linkedlist.iterator();
while(it.hasNext()){
it.next();
}
end2 = System.nanoTime();
System.out.println("LinkedList iterator "+count+" String, costs time "+(end2-begin2));
}
}
用相同量的数据测试ArrayList与LinkedList的插入耗时、索引取值、Iterator迭代取值的耗时,结果如下:
10000 | |||
插入耗时 | 根据index遍历耗时 | 根据iterator迭代遍历耗时 | |
ArrayList | 1290209 | 1296561 | 3139019 |
LinkedList | 2836015 | 61484786 | 2747069 |
100000 | |||
插入耗时 | 根据index遍历耗时 | 根据iterator迭代遍历耗时 | |
ArrayList | 6552695 | 1038032 | 5426695 |
LinkedList | 12740320 | 9673023061 | 2598010 |
1000000 | |||
插入耗时 | 根据index遍历耗时 | 根据iterator迭代遍历耗时 | |
ArrayList | 48045603 | 5982364 | 47055954 |
LinkedList | 243645601 | 1432633625275 | 15298744 |
1秒=1000豪秒 1毫秒=1000微秒 1微秒=1000毫微秒 1毫微秒=1纳秒 1纳秒=10埃秒 数值都是毫微秒, 1秒=1000毫秒*1000微秒=1000000毫微秒 |
1、同样多的数据,LinkedList的耗时反倒更大。
2、进行index索引遍历时,数据量越大,LinkedList的效率就越更低。
3、在使用Iterator进行遍历集合内容时,LinkedList的的效率更优,但是优势不明显。
结论:
以后尽量用ArrayList了。
相关文章推荐
- Java容器(二)——「ArrayList、LinkedList性能测试与分析」
- Java学习之为什么说LinkedList的插入和删除性能比ArrayList好,而ArrayList的查询更好?
- Java使用Arrays、ArrayList、LinkedList、Vector实现插入查询性能分析
- JAVA中LinkedList和ArrayList的使用及性能分析
- 比较Java数组,ArrayList,LinkedList,Vector 性能比较
- JAVA LinkedList和ArrayList的使用及性能分析
- JAVA LinkedList和ArrayList的使用及性能分析
- Java 集合系列08之 List总结(LinkedList, ArrayList等使用场景和性能分析)
- Java 集合系列08之 List总结(LinkedList, ArrayList等使用场景和性能分析)
- ArrayList和LinkedList性能对比测试
- Java Collections Framework - Java集合框架之List篇 ArrayList与LinkedList等的性能比较
- JAVA LinkedList和ArrayList的使用及性能分析
- (9) java ---- 集合框架图 ArrayList,LinkedList,Vector各自的性能特点及区别
- Java 集合系列之 List总结(LinkedList, ArrayList等使用场景和性能分析)
- ArrayList和LinkedList区别及性能测试
- Java 深入学习(2) —— ArrayList、LinkedList、Vector 应用场景与性能分析
- Java优化编程--核心类与性能 Vector ArrayList LinkedList String
- Java中arraylist和linkedlist源码分析与性能比较
- ArrayList 和 LinkedList 性能测试
- Java 集合系列08之 List总结(LinkedList, ArrayList等使用场景和性能分析)