1. 程式人生 > >ArrayList和LinkedList效能比較

ArrayList和LinkedList效能比較

ArrayList的本質上是一個數組,可以通過索引直接訪問元素.
LinkedList的本質上是一個連結串列,連結串列是無法通過索引直接訪問的,LinkedList通過索引訪問屬於間接訪問,也就是通過遍歷來獲取索引處的值,因此其效率相對較低,下面通過程式碼進行測試.

新增,查詢,刪除測試

新增

//新增元素比較
//ArrayList
ArrayList<Integer> arrayList =  new ArrayList<Integer>();       
System.out.println("新增元素測試");

startTime = new Date().
getTime(); for(int i = 0 ;i < 100000 ;i++) { arrayList.add(i); } endTime = new Date().getTime(); System.out.println("ArrayList新增10 0000個元素所用時間:" + (endTime-startTime) + "ms"); //ArrayList LinkedList<Integer> linkedList = new LinkedList<Integer>(); startTime = new Date().getTime(); for(int i =
0 ;i < 100000 ;i++) { linkedList.add(i); } endTime = new Date().getTime(); System.out.println("LinkedList新增10 0000個元素所用時間:" + (endTime-startTime) + "ms");

輸出

新增元素測試
ArrayList新增10 0000個元素所用時間:11ms
LinkedList新增10 0000個元素所用時間:44ms

從結果上看,由於LinkedList新增元素時需要管理next和first的指向,因此效率會高一點.ArrayList可使用索引直接訪問,效率很高.

查詢

//查詢元素比較
//ArrayList System.out.println("查詢元素測試"); startTime = new Date().getTime(); for(int i = 0 ;i < 100000 ;i++) { arrayList.get(i); } endTime = new Date().getTime(); System.out.println("ArrayList查詢10 0000個元素所用時間:" + (endTime-startTime) + "ms"); //ArrayList startTime = new Date().getTime(); for(int i = 0 ;i < 100000 ;i++) { linkedList.get(i); } endTime = new Date().getTime(); System.out.println("LinkedList查詢10 0000個元素所用時間:" + (endTime-startTime) + "ms");

輸出

查詢元素測試
ArrayList查詢10 0000個元素所用時間:1ms
LinkedList查詢10 0000個元素所用時間:5157ms

由於使用索引進行查詢時,LinkedList需要進行遍歷獲取索引位置,因此效率非常的慢.

刪除

//刪除元素比較
//ArrayList

System.out.println("刪除元素測試");
System.out.println("arrayList.size = " + arrayList.size() + "   linkedList.size = " + linkedList.size());
startTime = new Date().getTime();
for(int i = 0 ;i < 10000 ;i++) {
    arrayList.remove(i);
}
endTime = new Date().getTime();
System.out.println("ArrayList刪除10 000個元素所用時間:" + (endTime-startTime) + "ms");
//ArrayList

startTime = new Date().getTime();
for(int i = 0 ;i < 10000 ;i++) {
linkedList.remove(i);
}
endTime = new Date().getTime();
System.out.println("LinkedList刪除10 000個元素所用時間:" + (endTime-startTime) + "ms");

輸出

刪除元素測試
arrayList.size = 100000   linkedList.size = 100000
ArrayList刪除10 000個元素所用時間:254ms
LinkedList刪除10 000個元素所用時間:155ms

由於ArrayList本質上是陣列,因此刪除元素時該元素後面的元素都要進行移動,
而LinkedList先是定位元素位置,然後管理next和prev指向即可.而且ArrayList的刪除操作隨著資料量增大而操作時間有明顯的提高.因此刪除的效率ArrayList是非常低的

操作 ArrayList LinkedList 資料量 備註
新增 14 ms 36ms 5 0000 for(int i = 0 ;i < maxCount ;i++)末尾新增
新增 23 ms 31ms 10 0000 for(int i = 0 ;i < maxCount ;i++)末尾新增
新增 300 ms 8ms 5 0000 for(int i = 0 ;i < maxCount ;i++)開頭新增
新增 1035 ms 18ms 10 0000 for(int i = 0 ;i < maxCount ;i++)開頭新增
查詢 11 2329 5 0000 for(int i = 0 ;i < maxCount ;i++)
查詢 11 5557 10 0000 for(int i = 0 ;i < maxCount ;i++)
查詢 6 8 5 0000 for(Integer item:arrayList)
查詢 11 9 10 0000 for(Integer item:arrayList)
查詢 4 4 5 0000 迭代器while (linkedListIterator.hasNext())
查詢 11 8 10 00000 迭代器while (linkedListIterator.hasNext())
刪除 277 7 5 0000 for(int i = 0 ;i < maxCount ;i++) 移除索引0
刪除 1043 10 10 0000 for(int i = 0 ;i < maxCount ;i++)移除索引0

對於新增元素,如果對元素順序沒有要求,兩者的效率並不差多少;
對於查詢元素,如果使用for迴圈,那麼LinkedList的效率將會非常的差,但使用迭代器訪問,那麼相差不大.
對於刪除元素.由於需要移動元素ArrayList的效率也會很差.

因此如果應用中查詢比較多,建議使用ArrayList;
如果應用中刪除比較多,建議使用LinkList;

相關文章