测试代码
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
LinkedList<Integer> linkedList = new LinkedList<Integer>();
int size = 10000 * 1000;
int index = 5000 * 1000;
System.out.println("arrayList add " + size);
addData(arrayList, size);
System.out.println("linkedList add " + + size);
addData(linkedList, size);
System.out.println();
System.out.println("arrayList get " + index + " th");
getIndex(arrayList, index);
System.out.println("linkedList get " + index + " th");
getIndex(linkedList, index);
System.out.println();
System.out.println("arrayList set " + index + " th");
setIndex(arrayList, index);
System.out.println("linkedList set " + index + " th");
setIndex(linkedList, index);
System.out.println();
System.out.println("arrayList add " + index + " th");
addIndex(arrayList, index);
System.out.println("linkedList add " + index + " th");
addIndex(linkedList, index);
System.out.println();
System.out.println("arrayList remove " + index + " th");
removeIndex(arrayList, index);
System.out.println("linkedList remove " + index + " th");
removeIndex(linkedList, index);
System.out.println();
System.out.println("arrayList remove Object " + index);
removeObject(arrayList, (Object)index);
System.out.println("linkedList remove Object " + index);
removeObject(linkedList, (Object)index);
System.out.println();
System.out.println("arrayList add");
add(arrayList);
System.out.println("linkedList add");
add(linkedList);
System.out.println();
System.out.println("arrayList foreach");
foreach(arrayList);
System.out.println("linkedList foreach");
foreach(linkedList);
System.out.println();
System.out.println("arrayList forSize");
forSize(arrayList);
System.out.println("linkedList forSize");
// forSize(linkedList);
System.out.println("cost time: ...");
System.out.println();
System.out.println("arrayList iterator");
ite(arrayList);
System.out.println("linkedList iterator");
ite(linkedList);
}
private static void addData(List<Integer> list, int size) {
long s1 = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
list.add(i);
}
long s2 = System.currentTimeMillis();
System.out.println("cost time: " + (s2-s1));
}
private static void getIndex(List<Integer> list, int index) {
long s1 = System.currentTimeMillis();
list.get(index);
long s2 = System.currentTimeMillis();
System.out.println("cost time: " + (s2-s1));
}
private static void setIndex(List<Integer> list, int index) {
long s1 = System.currentTimeMillis();
list.set(index, 1024);
long s2 = System.currentTimeMillis();
System.out.println("cost time: " + (s2-s1));
}
private static void addIndex(List<Integer> list, int index) {
long s1 = System.currentTimeMillis();
list.add(index, 1024);
long s2 = System.currentTimeMillis();
System.out.println("cost time: " + (s2-s1));
}
private static void removeIndex(List<Integer> list, int index) {
long s1 = System.currentTimeMillis();
list.remove(index);
long s2 = System.currentTimeMillis();
System.out.println("cost time: " + (s2-s1));
}
private static void removeObject(List<Integer> list, Object obj) {
long s1 = System.currentTimeMillis();
list.remove(obj);
long s2 = System.currentTimeMillis();
System.out.println("cost time: " + (s2-s1));
}
private static void add(List<Integer> list) {
long s1 = System.currentTimeMillis();
list.add(1024);
long s2 = System.currentTimeMillis();
System.out.println("cost time: " + (s2-s1));
}
private static void foreach(List<Integer> list) {
long s1 = System.currentTimeMillis();
for (Integer i : list) {
//do nothing
}
long s2 = System.currentTimeMillis();
System.out.println("cost time: " + (s2-s1));
}
private static void forSize(List<Integer> list) {
long s1 = System.currentTimeMillis();
int size = list.size();
for (int i = 0; i < size; i++) {
list.get(i);
}
long s2 = System.currentTimeMillis();
System.out.println("cost time: " + (s2-s1));
}
private static void ite(List<Integer> list) {
long s1 = System.currentTimeMillis();
Iterator<Integer> ite = list.iterator();
while (ite.hasNext()) {
ite.next();
}
long s2 = System.currentTimeMillis();
System.out.println("cost time: " + (s2-s1));
}
JDK1.8,win7 64位。结果
arrayList add 10000000
cost time: 3309
linkedList add 10000000
cost time: 1375
arrayList get 5000000 th
cost time: 0
linkedList get 5000000 th
cost time: 53
arrayList set 5000000 th
cost time: 0
linkedList set 5000000 th
cost time: 44
arrayList add 5000000 th
cost time: 3
linkedList add 5000000 th
cost time: 45
arrayList remove 5000000 th
cost time: 3
linkedList remove 5000000 th
cost time: 46
arrayList remove Object 5000000
cost time: 31
linkedList remove Object 5000000
cost time: 131
arrayList add
cost time: 0
linkedList add
cost time: 0
arrayList foreach
cost time: 30
linkedList foreach
cost time: 128
arrayList forSize
cost time: 5
linkedList forSize
cost time: ...
arrayList iterator
cost time: 6
linkedList iterator
cost time: 113
思考:
源码分析参考:
https://blog.csdn.net/luyuqin0115/article/details/80395694
ConstXiong 备案号:苏ICP备16009629号-3