精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2013-05-21
时间单位:毫秒 一万条数据测试: ArrayList中插入10000条数据,共花费:3 LinkedList中插入10000条数据,共花费:5 ArrayList中遍历全部数据,共花费:2 LinkedList中遍历全部数据,共花费:136 ArrayList中随机访问1000数据,共花费:1 LinkedList中随机访问1000数据,共花费:15 ArrayList中随机插入1000数据,共花费:5 LinkedList中随机插入1000数据,共花费:15 ArrayList中随机删除1000数据,共花费:5 LinkedList中随机删除1000数据,共花费:15 十万条数据测试: ArrayList中插入100000条数据,共花费:18 LinkedList中插入100000条数据,共花费:38 ArrayList中遍历全部数据,共花费:6 LinkedList中遍历全部数据,共花费:12875 ArrayList中随机访问1000数据,共花费:0 LinkedList中随机访问1000数据,共花费:155 ArrayList中随机插入1000数据,共花费:43 LinkedList中随机插入1000数据,共花费:156 ArrayList中随机删除1000数据,共花费:42 LinkedList中随机删除1000数据,共花费:158 百万条数据测试 ArrayList中插入1000000条数据,共花费:186 LinkedList中插入1000000条数据,共花费:435 ArrayList中遍历全部数据,共花费:19 LinkedList在遍历全部数据时,超过10分钟还未遍历完,直接结束程序了 测试代码: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Random; public class TestList { private static int total = 1000000; private static List<Integer> aList = new ArrayList<Integer>();//后面再演示先定义长度的 private static List<Integer> lList = new LinkedList<Integer>(); /** * 演示批量插入数据 */ private static void testBatchInsert() { Random r = new Random(); //测试ArrayList long beginTime = System.currentTimeMillis(); for (int i = 0; i < total; i++) { aList.add(r.nextInt());//在最后插入数据,使用add()方法 } long endTime = System.currentTimeMillis(); System.out.println("ArrayList中插入" + total + "条数据,共花费:" + (endTime - beginTime)); //测试LinkedList beginTime = System.currentTimeMillis(); for (int i = 0; i < total; i++) { lList.add(r.nextInt());//在最后插入数据,使用add()方法 } endTime = System.currentTimeMillis(); System.out.println("LinkedList中插入" + total + "条数据,共花费:" + (endTime - beginTime)); } /** * 测试遍历访问全部数据 */ private static void testTraverseAccess() { //测试ArrayList long beginTime = System.currentTimeMillis(); for (int i = 0; i < aList.size(); i++) { aList.get(i);//通过get(int i)方法获得 } long endTime = System.currentTimeMillis(); System.out.println("ArrayList中遍历全部数据,共花费:" + (endTime - beginTime)); //测试LinkedList beginTime = System.currentTimeMillis(); for (int i = 0; i < lList.size(); i++) { lList.get(i);//通过get(int i)方法获得 } endTime = System.currentTimeMillis(); System.out.println("LinkedList中遍历全部数据,共花费:" + (endTime - beginTime)); } /** * 测试随机访问数据 */ private static void testRandomAccess() { int num = 1000; Random r = new Random(); //测试ArrayList long beginTime = System.currentTimeMillis(); for(int i = 0; i < num; i++) { aList.get(r.nextInt(aList.size())); } long endTime = System.currentTimeMillis(); System.out.println("ArrayList中随机访问" + num + "数据,共花费:" + (endTime - beginTime)); //测试LinkedList beginTime = System.currentTimeMillis(); for(int i = 0; i < num; i++) { lList.get(r.nextInt(aList.size())); } endTime = System.currentTimeMillis(); System.out.println("LinkedList中随机访问" + num + "数据,共花费:" + (endTime - beginTime)); } /** * 演示随机插入数据 */ private static void testRandomInsert() { int num = 1000; Random r = new Random(); //测试ArrayList long beginTime = System.currentTimeMillis(); for(int i = 0; i < num; i++) { //通过list.add(int index, Integer element)在指定位置插入数据 aList.add(r.nextInt(aList.size()), r.nextInt()); } long endTime = System.currentTimeMillis(); System.out.println("ArrayList中随机插入" + num + "数据,共花费:" + (endTime - beginTime)); //测试LinkedList beginTime = System.currentTimeMillis(); for(int i = 0; i < num; i++) { //通过list.add(int index, Integer element)在指定位置插入数据 lList.add(r.nextInt(lList.size()), r.nextInt()); } endTime = System.currentTimeMillis(); System.out.println("LinkedList中随机插入" + num + "数据,共花费:" + (endTime - beginTime)); } /** * 演示随机删除数据 */ private static void testRandomDelete() { int num = 1000; Random r = new Random(); //测试ArrayList long beginTime = System.currentTimeMillis(); for(int i = 0; i < num; i++) { //通过list.remove(int index)在指定位置删除数据 aList.remove(r.nextInt(aList.size())); } long endTime = System.currentTimeMillis(); System.out.println("ArrayList中随机删除" + num + "数据,共花费:" + (endTime - beginTime)); //测试LinkedList beginTime = System.currentTimeMillis(); for(int i = 0; i < num; i++) { //通过list.remove(int index)在指定位置删除数据 lList.remove(r.nextInt(lList.size())); } endTime = System.currentTimeMillis(); System.out.println("LinkedList中随机删除" + num + "数据,共花费:" + (endTime - beginTime)); } public static void main(String[] args) { testBatchInsert(); testTraverseAccess(); testRandomAccess(); testRandomInsert(); testRandomDelete(); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2013-05-21
考虑了一下,LinkedList随机增删慢的原因应该是因为查找太慢了
|
|
返回顶楼 | |
发表时间:2013-05-21
个人意见,还是应当先根据代码,分析两种容器插入和遍历的逻辑,对对比测试的结果进行预计,然后看实际代码测试的结果,来验证根据代码得出的结论,这样才有意义和价值。
|
|
返回顶楼 | |
发表时间:2013-05-21
ArrayList 只是对象数组实现的list
LinkedList 不只是一个List ,还要支持队列和双端队列, 功能能强大,带来的就是要维护的指针索引更多,所以慢是正常的。 使用哪个要看实际需求啊。 |
|
返回顶楼 | |
发表时间:2013-05-22
楼主突破性的结论来自于搞笑的测试。你根据index来操作list,LinkedList会哭的。要知道ArrayList是RandomAccess的。
|
|
返回顶楼 | |
发表时间:2013-05-22
LinkedList用get方法会让你郁闷的……你直接foreach一下应该和ArrayList无差
|
|
返回顶楼 | |
发表时间:2013-05-22
呃,拿来ArrayList的优势去对比LinkedArrayList的弱势。。。。
测试方法有点问题,不能使用在LinkedArrayList中使用GET速度觉得起不来。 完全没有对比性 。。 |
|
返回顶楼 | |
发表时间:2013-05-22
最后修改:2013-05-22
不试试删除操作?
好吧。。。之前没仔细看。。话说用index操作linkedList。。。不慢死你才怪。。。。 非随机删除你试试? |
|
返回顶楼 | |
发表时间:2013-05-22
代码我不就仔细看了,我是冲着标题进来的。linklist和arraylist,数据结构决定了二者的性能区别。其它的就不说了!
|
|
返回顶楼 | |
发表时间:2013-05-22
你用get跟下标来操作linkedlist。。。我靠。。
你用iterator的话性能跟array应该差不多的 |
|
返回顶楼 | |