ArrayList和LinkedList性能对比测试
2016-11-04 00:40
459 查看
package com.test.api; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class TestList { <span style="white-space:pre"> </span>/** <span style="white-space:pre"> </span> * @author Administrator <span style="white-space:pre"> </span> * @date 2016-11-3下午11:49:12 <span style="white-space:pre"> </span> * @功能: List 有序,可重复 1.ArrayList查询很快,但增删较慢 --底层的数据结构使用的是数组结构 取代Vector <span style="white-space:pre"> </span> * 查询还是增删都很慢 2.LinkedList 查询较慢,增删较快 --底层的数据结构是链表结构 <span style="white-space:pre"> </span> */ <span style="white-space:pre"> </span>public static void main(String[] args) { <span style="white-space:pre"> </span>int i = 0; <span style="white-space:pre"> </span>long t1, t2, t3, t4, t5, t6; <span style="white-space:pre"> </span>// ArrayList <span style="white-space:pre"> </span>List<String> list = new ArrayList<String>(); <span style="white-space:pre"> </span>t1 = System.currentTimeMillis(); <span style="white-space:pre"> </span>for (i = 0; i < 53140; i++) { <span style="white-space:pre"> </span>list.add("a" + i); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>t2 = System.currentTimeMillis(); <span style="white-space:pre"> </span>for (i = 0; i < list.size(); i++) { <span style="white-space:pre"> </span>list.get(i); <span style="white-space:pre"> </span>System.out.println(list.get(i)); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>t3 = System.currentTimeMillis(); <span style="white-space:pre"> </span>// LinkedList <span style="white-space:pre"> </span>List<String> listlink = new LinkedList<String>(); <span style="white-space:pre"> </span>t4 = System.currentTimeMillis(); <span style="white-space:pre"> </span>for (i = 0; i < 53140; i++) { <span style="white-space:pre"> </span>listlink.add("b" + i); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>t5 = System.currentTimeMillis(); <span style="white-space:pre"> </span>for (i = 0; i < listlink.size(); i++) { <span style="white-space:pre"> </span>listlink.get(i); <span style="white-space:pre"> </span>System.out.println(listlink.get(i)); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>t6 = System.currentTimeMillis(); <span style="white-space:pre"> </span>System.out.println("Run Time ArrayList:add " + (t2 - t1) + "(ms)"); <span style="white-space:pre"> </span>System.out.println("Run Time ArrayList:query " + (t3 - t2) + "(ms)"); <span style="white-space:pre"> </span>System.out.println("Run Time LinkedList: add " + (t5 - t4) + "(ms)"); <span style="white-space:pre"> </span>System.out.println("Run Time LinkedList: query " + (t6 - t5) + "(ms)"); <span style="white-space:pre"> </span>} }
*运行结果:
Run Time ArrayList:add 38(ms)
Run Time ArrayList:query 303(ms)
Run Time LinkedList: add 12(ms)
Run Time LinkedList: query 11075(ms)
很显然,在常规情况下ArrayList 比LinkedList好用。
LinkedList的查询太慢了,而ArrayList增加却不会比LinkedList差太离谱。
相关文章推荐
- 对比ArrayList和LinkedList 使用普通循环和迭代循环的性能
- 测试java的ArrayList与LinkedList的性能
- ArrayList和LinkedList性能测试
- ArrayList和LinkedList的操作性能对比
- ArrayList和LinkedList区别及性能测试
- Linkedlist与ArrayList的各种操作性能对比-单线程对比
- ArrayList和LinkedList遍历方式及性能对比分析
- ArrayList 和 LinkedList 性能测试
- Java容器(二)——「ArrayList、LinkedList性能测试与分析」
- 关于List接口中ArrayList,LinkedList和Vector的取值方法的一些性能测试
- ArrayList和LinkedList的操作性能对比
- Arraylist和linkedlist的性能测试
- 关于ArrayList与LinkedList插入元素性能的对比
- ArrayList和LinkedList的操作性能对比
- ArrayList和LinkedList遍历方式及性能对比分析
- ArrayList及LinkedList遍历方式及性能比较
- java性能优化-Arraylist与Linkedlist整改查性能比较
- Java 集合系列08之 List总结(LinkedList, ArrayList等使用场景和性能分析)
- Java 集合系列 07 List总结(LinkedList, ArrayList等使用场景和性能分析)
- paip.提升性能---list,arraylist,vector,linkedlist,map的选用..