在正常的数据结构中:
LinkedList对增加和删除操作比ArrayList的性能要好
ArrayList则更擅长做查找遍历操作
但是今天在阅读ArrayList源码时发现,ArrayList的增加和删除操作是用native方法实现的,所以其真实性能可能会较想象中的好一些,所以今天就用jdk中提供的两个容器做一个性能比对
- 环境 jdk 1.8
-
idea
1w次
times = 10000
*****************************
测试:尾部增加元素
ArrayList -----> 6047032
LinkedList -----> 5327290
*****************************
*****************************
测试:尾部删除元素
ArrayList -----> 2217388
LinkedList -----> 1342834
*****************************
*****************************
测试:头增加元素
ArrayList -----> 11537687
LinkedList -----> 4129430
*****************************
*****************************
测试:头删除元素
ArrayList -----> 2004844
LinkedList -----> 784318
*****************************
*****************************
测试:随机位置增加元素
ArrayList -----> 17337535
LinkedList -----> 6830068
*****************************
*****************************
测试:随机位置查找元素
ArrayList -----> 5874260
LinkedList -----> 8369624
*****************************
*****************************
测试:随机位置更改元素
ArrayList -----> 4312895
LinkedList -----> 6101773
*****************************
*****************************
测试:随机位置删除元素
ArrayList -----> 29385414
LinkedList -----> 8368768
*****************************
Process finished with exit code 0
10w次
times = 100000
*****************************
测试:尾部增加元素
ArrayList -----> 24804808
LinkedList -----> 43177695
*****************************
*****************************
测试:尾部删除元素
ArrayList -----> 38577416
LinkedList -----> 11566340
*****************************
*****************************
测试:头增加元素
ArrayList -----> 1956851814
LinkedList -----> 40207634
*****************************
*****************************
测试:头删除元素
ArrayList -----> 2967494
LinkedList -----> 4663143
*****************************
*****************************
测试:随机位置增加元素
ArrayList -----> 1273261296
LinkedList -----> 10900909
*****************************
*****************************
测试:随机位置查找元素
ArrayList -----> 5205409
LinkedList -----> 4912038
*****************************
*****************************
测试:随机位置更改元素
ArrayList -----> 8216096
LinkedList -----> 7091364
*****************************
*****************************
测试:随机位置删除元素
ArrayList -----> 1102387723
LinkedList -----> 18923705
*****************************
Process finished with exit code 0
50w次
times = 500000
*****************************
测试:尾部增加元素
ArrayList -----> 102073824
LinkedList -----> 109622779
*****************************
*****************************
测试:尾部删除元素
ArrayList -----> 36268510
LinkedList -----> 12494350
*****************************
*****************************
测试:头增加元素
ArrayList -----> 34781611889
LinkedList -----> 118171589
*****************************
*****************************
测试:头删除元素
ArrayList -----> 21939523
LinkedList -----> 8944819
*****************************
*****************************
测试:随机位置增加元素
ArrayList -----> 29949963586
LinkedList -----> 45516108
*****************************
*****************************
测试:随机位置查找元素
ArrayList -----> 30674364
LinkedList -----> 30298028
*****************************
*****************************
测试:随机位置更改元素
ArrayList -----> 39145769
LinkedList -----> 39933936
*****************************
*****************************
测试:随机位置删除元素
ArrayList -----> 30271861584
LinkedList -----> 38692883
*****************************
Process finished with exit code 0
times = 1000000
*****************************
测试:尾部增加元素
ArrayList -----> 211529391
LinkedList -----> 194531840
*****************************
*****************************
测试:尾部删除元素
ArrayList -----> 38223319
LinkedList -----> 42298009
*****************************
*****************************
测试:头增加元素
ArrayList -----> 141881889993
LinkedList -----> 55003192
*****************************
*****************************
测试:头删除元素
ArrayList -----> 25792262
LinkedList -----> 27698745
*****************************
*****************************
测试:随机位置增加元素
ArrayList -----> 143949528233
LinkedList -----> 65495692
*****************************
*****************************
测试:随机位置查找元素
ArrayList -----> 41698438
LinkedList -----> 38416191
*****************************
*****************************
测试:随机位置更改元素
ArrayList -----> 1233481744
LinkedList -----> 59149302
*****************************
*****************************
测试:随机位置删除元素
ArrayList -----> 142282579885
LinkedList -----> 50594504
*****************************
源码
import java.util.*;
public class Main {
public static void main(String[] args) {
Random random = new Random(1000);
Random random1 = new Random(10);
ArrayList<Integer> arrayList = new ArrayList();
LinkedList<Integer> linkedList = new LinkedList<>();
long startTime;
long endTime;
int times = 10000;
System.out.println("times = " + times);
System.out.println("*****************************");
System.out.println("测试:尾部增加元素");
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
arrayList.add(arrayList.size(), random.nextInt());
}
endTime = System.nanoTime();
System.out.println("ArrayList -----> " + (endTime - startTime));
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
linkedList.add(random.nextInt());
}
endTime = System.nanoTime();
System.out.println("LinkedList -----> " + (endTime - startTime));
System.out.println("*****************************");
System.out.println();
System.out.println("*****************************");
System.out.println("测试:尾部删除元素");
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
arrayList.remove(arrayList.size() - 1);
}
endTime = System.nanoTime();
System.out.println("ArrayList -----> " + (endTime - startTime));
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
linkedList.removeLast();
}
endTime = System.nanoTime();
System.out.println("LinkedList -----> " + (endTime - startTime));
System.out.println("*****************************");
System.out.println();
System.out.println("*****************************");
System.out.println("测试:头增加元素");
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
arrayList.add(0, random.nextInt());
}
endTime = System.nanoTime();
System.out.println("ArrayList -----> " + (endTime - startTime));
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
linkedList.add(0, random.nextInt());
}
endTime = System.nanoTime();
System.out.println("LinkedList -----> " + (endTime - startTime));
System.out.println("*****************************");
System.out.println();
System.out.println("*****************************");
System.out.println("测试:头删除元素");
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
arrayList.remove(arrayList.size() - 1);
}
endTime = System.nanoTime();
System.out.println("ArrayList -----> " + (endTime - startTime));
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
linkedList.removeLast();
}
endTime = System.nanoTime();
System.out.println("LinkedList -----> " + (endTime - startTime));
System.out.println("*****************************");
System.out.println();
System.out.println("*****************************");
System.out.println("测试:随机位置增加元素");
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
arrayList.add(Math.abs(arrayList.size() / random1.nextInt()), random.nextInt());
}
endTime = System.nanoTime();
System.out.println("ArrayList -----> " + (endTime - startTime));
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
linkedList.add(Math.abs(linkedList.size()/random1.nextInt()), random.nextInt());
}
endTime = System.nanoTime();
System.out.println("LinkedList -----> " + (endTime - startTime));
System.out.println("*****************************");
System.out.println();
System.out.println("*****************************");
System.out.println("测试:随机位置查找元素");
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
arrayList.get(Math.abs(arrayList.size()/random1.nextInt()));
}
endTime = System.nanoTime();
System.out.println("ArrayList -----> " + (endTime - startTime));
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
linkedList.get(Math.abs(linkedList.size()/random1.nextInt()));
}
endTime = System.nanoTime();
System.out.println("LinkedList -----> " + (endTime - startTime));
System.out.println("*****************************");
System.out.println();
System.out.println("*****************************");
System.out.println("测试:随机位置更改元素");
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
arrayList.set(Math.abs(arrayList.size()/random1.nextInt()), random.nextInt());
}
endTime = System.nanoTime();
System.out.println("ArrayList -----> " + (endTime - startTime));
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
linkedList.set(Math.abs(linkedList.size()/random1.nextInt()), random.nextInt());
}
endTime = System.nanoTime();
System.out.println("LinkedList -----> " + (endTime - startTime));
System.out.println("*****************************");
System.out.println();
System.out.println("*****************************");
System.out.println("测试:随机位置删除元素");
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
arrayList.remove(Math.abs(arrayList.size()/random1.nextInt()));
}
endTime = System.nanoTime();
System.out.println("ArrayList -----> " + (endTime - startTime));
startTime = System.nanoTime();
for (int i = 0; i < times; i++){
linkedList.remove(Math.abs(linkedList.size()/random1.nextInt()));
}
endTime = System.nanoTime();
System.out.println("LinkedList -----> " + (endTime - startTime));
System.out.println("*****************************");
System.out.println();
}
}