资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
.主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论。通过本文你可以了解(1)List的五种遍历方式及各自性能 (2)foreach及Iterator的实现 (3)加深对ArrayList和LinkedList实现的了解。阅读本文前希望你已经了解ArrayList顺序存储和LinkedList链式的结构,本文不对此进行介绍。相关:HashMap循环遍历方式及其性能对比1. List的五种遍历方式下面只是简单介绍各种遍历示例(以ArrayList为例),各自优劣会在本文后面进行分析给出结论。(1) for each循环Java1234List list = new ArrayList();for (Integer j : list) / use j(2) 显示调用集合迭代器Java1234List list = new ArrayList();for (Iterator iterator = list.iterator(); iterator.hasNext();) iterator.next();或Java12345List list = new ArrayList();Iterator iterator = list.iterator();while (iterator.hasNext() iterator.next();(3) 下标递增循环,终止条件为每次调用size()函数比较判断Java1234List list = new ArrayList();for (int j = 0; j list.size(); j+) list.get(j);(4) 下标递增循环,终止条件为和等于size()的临时变量比较判断Java12345List list = new ArrayList();int size = list.size();for (int j = 0; j size; j+) list.get(j);(5) 下标递减循环Java1234List list = new ArrayList();for (int j = list.size() - 1; j = 0; j-) list.get(j);在测试前大家可以根据对ArrayList和LinkedList数据结构及Iterator的了解,想想上面五种遍历方式哪个性能更优。2、List五种遍历方式的性能测试及对比以下是性能测试代码,会输出不同数量级大小的ArrayList和LinkedList各种遍历方式所花费的时间。ArrayList和LinkedList循环性能对比测试代码PS:如果运行报异常in thread “main” java.lang.OutOfMemoryError: Java heap space,请将main函数里面list size的大小减小。其中getArrayLists函数会返回不同size的ArrayList,getLinkedLists函数会返回不同size的LinkedList。loopListCompare函数会分别用上面的遍历方式1-5去遍历每一个list数组(包含不同大小list)中的list。print开头函数为输出辅助函数。测试环境为Windows7 32位系统 3.2G双核CPU 4G内存,Java 7,Eclipse -Xms512m -Xmx512m最终测试结果如下:1234567891011121314151617181920212223242526272829compare loop performance of ArrayList-list size| 10,000| 100,000 | 1,000,000 | 10,000,000 -for each | 1 ms| 3 ms| 14 ms | 152 ms-for iterator | 0 ms| 1 ms| 12 ms | 114 ms-for list.size()| 1 ms| 1 ms| 13 ms | 128 ms-for size = list.size() | 0 ms| 0 ms| 6 ms| 62 ms -for j-| 0 ms| 1 ms| 6 ms| 63 ms -compare loop performance of LinkedList-list size| 100 | 1,000 | 10,000| 100,000 -for each | 0 ms| 1 ms| 1 ms| 2 ms-for iterator | 0 ms| 0 ms| 0 ms| 2 ms-for list.size()| 0 ms| 1 ms| 73 ms | 7972 ms -for size = list.size() | 0 ms| 0 ms| 67 ms | 8216 ms -for j-| 0 ms| 1 ms| 67 ms | 8277 ms -第一张表为ArrayList对比结果,第二张表为LinkedList对比结果。表横向为同一遍历方式不同大小list遍历的时间消耗,纵向为同一list不同遍历方式遍历的时间消耗。PS:由于首次遍历List会稍微多耗时一点,for each的结果稍微有点偏差,将测试代码中的几个Type顺序调换会发现,for each耗时和for iterator接近。3、遍历方式性能测试结果分析(1) foreach介绍foreach是Java SE5.0引入的功能很强的循环结构,for (Integer j : list)应读作for each int in list。for (Integer j : list)实现几乎等价于Java1234Iterator iterator = list.iterator();while(iterator.hasNext() Integer j = iterator.next();下面的分析会将foreach和显示调用集合迭代器两种遍历方式归类为Iterator方式,其他三种称为get方式遍历。这时我们已经发现foreach的一大好处,简单一行实现了四行的功能,使得代码简洁美观,另一大好处是相对于下标循环而言的,foreach不必关心下标初始值和终止值及越界等,所以不易出错。Effective-Java中推荐使用此种写法遍历,本文会验证这个说法。使用foreach结构的类对象必须实现了Iterable接口,Java的Collection继承自此接口,List实现了Collection,这个接口仅包含一个函数,源码如下:Java123456789101112131415161718192021package java.lang;import java.util.Iterator;/* * Implementing this interface allows an object to be the target of * the foreach statement. * * param the type of elements returned by the iterator * * since 1.5 */public interfac
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号