ArrayList和LinkedList循环遍历效率探索(一)

一、Arraylist的遍历方式效率比较

实验代码:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestListSpeed {

public static void main(String[] args) {
List<Integer> list = new ArrayList<>();

for (int index = 0; index < 10000000; index++) {
list.add(index);
}

long start = System.currentTimeMillis();
for (int index = 0; index < list.size(); index++) {
Integer integer = list.get(index);
}
long end = System.currentTimeMillis();
System.out.println("for循环遍历结果:"+(end - start));

start = System.currentTimeMillis();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Integer integer = (Integer) iterator.next();
}
end = System.currentTimeMillis();
System.out.println("for循环迭代器:"+(end - start));

start = System.currentTimeMillis();
for (Iterator<Integer> iterator = list.iterator(); iterator.hasNext();) {
Integer integer = (Integer) iterator.next();
}
end = System.currentTimeMillis();
System.out.println("for循环泛型迭代器:"+(end - start));

start = System.currentTimeMillis();
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
Integer integer = (Integer) iterator.next();
}
end = System.currentTimeMillis();
System.out.println("循环while泛型迭代器:"+(end - start));

start = System.currentTimeMillis();
for (Integer integer : list) {
Integer integer2 = integer;
}
end = System.currentTimeMillis();
System.out.println("foreach迭代:"+(end-start));
}
}

实验6次:结果分别是:

 

for循环遍历结果:25
for循环迭代器:29
for循环泛型迭代器:26
循环while泛型迭代器:26
foreach迭代:26

for循环遍历结果:23
for循环迭代器:27
for循环泛型迭代器:25
循环while泛型迭代器:24
foreach迭代:26

for循环遍历结果:23
for循环迭代器:27
for循环泛型迭代器:27
循环while泛型迭代器:26
foreach迭代:25

for循环遍历结果:22
for循环迭代器:27
for循环泛型迭代器:25
循环while泛型迭代器:26
foreach迭代:25

for循环遍历结果:24
for循环迭代器:26
for循环泛型迭代器:26
循环while泛型迭代器:24
foreach迭代:25

for循环遍历结果:22
for循环迭代器:28
for循环泛型迭代器:27
循环while泛型迭代器:25
foreach迭代:26

 

实验结论:

1.Arraylist采用普通for循环遍历速度最快,因为它内部是数组结构;次之,foreach循环,似乎稍微占点优势;再次之,while循环使用迭代器;再次之,for循环使用iterator泛型迭代器;最后,for循环使用iterator没有加泛型的迭代器。

注意:while循环使用迭代器比for循环使用迭代器在速度上似乎占点优势,但是for循环使用迭代器,迭代器在for循环结束后就成为垃圾被垃圾回收器回收,程序负担更小。但是java在编译过程中实际上可能会将while中的迭代器自动转换为for循环迭代器,有心者可以自己使用反编译工具查看编译文件。

2.数据量较小,遍历速度相差不大。

 

二、LinkedList遍历方式效率比较

将实验代码替换为Linkedlist,并且将添加中的for循环改为index<100000.

实验结果1:

for循环遍历结果:4508
for循环迭代器:3
for循环泛型迭代器:1
循环while泛型迭代器:1
foreach迭代:1

结论1:使用普通for循环效率极为低下,因为Linkedlist底层不是数组,而是链式结构。

 

将添加中的for循环恢复为index<10000000。并屏蔽掉普通for循环遍历。

实验结果2:、

 

for循环迭代器:102
for循环泛型迭代器:102
循环while泛型迭代器:104
foreach迭代:103

for循环迭代器:101
for循环泛型迭代器:105
循环while泛型迭代器:104
foreach迭代:103

for循环迭代器:103
for循环泛型迭代器:103
循环while泛型迭代器:105
foreach迭代:102

for循环迭代器:114
for循环泛型迭代器:114
循环while泛型迭代器:116
foreach迭代:115

for循环迭代器:103
for循环泛型迭代器:102
循环while泛型迭代器:103
foreach迭代:104

for循环迭代器:112
for循环泛型迭代器:113
循环while泛型迭代器:116
foreach迭代:115

 

结论2:最快,for循环迭代器(iterator未添加泛型)和for循环迭代器(iterator添加了泛型)加上foreach效率差不多;次之,while循环使用迭代器。

 

三、Arraylist和LinkedList遍历效率比较

绝对推荐Arraylist遍历元素,传说中Arraylist查询快,果然是名不虚传!

posted @ 2018-01-12 22:25  一肥惊人  阅读(1034)  评论(0编辑  收藏  举报