ArrayList和LinkedList的幾種循環遍歷方式及性能對比分析
主要介紹ArrayList和LinkedList這兩種list的五種循環遍歷方式,各種方式的性能測試對比,根據ArrayList和LinkedList的源碼實現分析性能結果,總結結論。
通過本文你可以瞭解(1)List的五種遍歷方式及各自性能 (2)foreach及Iterator的實現 (3)加深對ArrayList和LinkedList實現的瞭解。
閱讀本文前希望你已經瞭解ArrayList順序存儲和LinkedList鏈式的結構,本文不對此進行介紹。
1. List的五種遍歷方式
下面只是簡單介紹各種遍歷示例(以ArrayList爲例),各自優劣會在本文後面進行分析給出結論。
(1) for each循環
Java
List<Integer> list = new ArrayList<Integer>(); for (Integer j : list) { // use j }
1 2 3 4 |
List<Integer> list = new ArrayList<Integer>(); for (Integer j : list) { // use j } |
(2) 顯示調用集合迭代器
Java
List<Integer> list = new ArrayList<Integer>(); for (Iterator<Integer> iterator = list.iterator(); iterator.hasNext();) { iterator.next(); }
1 2 3 4 |
List<Integer> list = new ArrayList<Integer>(); for (Iterator<Integer> iterator = list.iterator(); iterator.hasNext();) { iterator.next(); } |
或
Java
List<Integer> list = new ArrayList<Integer>(); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { iterator.next(); }
1 2 3 4 5 |
List<Integer> list = new ArrayList<Integer>(); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { iterator.next(); } |
(3) 下標遞增循環,終止條件爲每次調用size()函數比較判斷
Java
List<Integer> list = new ArrayList<Integer>(); for (int j = 0; j < list.size(); j++) { list.get(j); }
1 2 3 4 |
List<Integer> list = new ArrayList<Integer>(); for (int j = 0; j < list.size(); j++) { list.get(j); } |
(4) 下標遞增循環,終止條件爲和等於size()的臨時變量比較判斷
Java
List<Integer> list = new ArrayList<Integer>(); int size = list.size(); for (int j = 0; j < size; j++) { list.get(j); }
1 2 3 4 5 |
List<Integer> list = new ArrayList<Integer>(); int size = list.size(); for (int j = 0; j < size; j++) { list.get(j); } |
(5) 下標遞減循環
Java
List<Integer> list = new ArrayList<Integer>(); for (int j = list.size() - 1; j >= 0; j--) { list.get(j); }
1 2 3 4 |
List<Integer> list = new ArrayList<Integer>(); for (int j = list.size() - 1; j >= 0; j--) { list.get(j); } |
在測試前大家可以根據對ArrayList和LinkedList數據結構及Iterator的瞭解,想想上面五種遍歷方式哪個性能更優。
2、List五種遍歷方式的性能測試及對比
以下是性能測試代碼,會輸出不同數量級大小的ArrayList和LinkedList各種遍歷方式所花費的時間。
ArrayList和LinkedList循環性能對比測試代碼
PS:如果運行報異常in thread “main” java.lang.OutOfMemoryError: Java heap space,請將main函數裏面list size的大小減小。
其中getArrayLists函數會返回不同size的ArrayList,getLinkedLists函數會返回不同size的LinkedList。
loopListCompare函數會分別用上面的遍歷方式1-5去遍歷每一個list數組(包含不同大小list)中的list。
print開頭函數爲輸出輔助函數。
測試環境爲Windows7 32位系統 3.2G雙核CPU 4G內存,Java 7,Eclipse -Xms512m -Xmx512m
最終測試結果如下:
compare loop performance of ArrayList ----------------------------------------------------------------------- list size | 10,000 | 100,000 | 1,000,000 | 10,000,000 ----------------------------------------------------------------------- for each | 1 ms | 3 ms | 14 ms | 152 ms ----------------------------------------------------------------------- for iterator | 0 ms | 1 ms | 12 ms | 114 ms ----------------------------------------------------------------------- for list.size() | 1 ms | 1 ms | 13 ms | 128 ms ----------------------------------------------------------------------- for size = list.size() | 0 ms | 0 ms | 6 ms | 62 ms ----------------------------------------------------------------------- for j-- | 0 ms | 1 ms | 6 ms | 63 ms ----------------------------------------------------------------------- compare loop performance of LinkedList ----------------------------------------------------------------------- list size | 100 | 1,000 | 10,000 | 100,000 ----------------------------------------------------------------------- for each | 0 ms | 1 ms | 1 ms | 2 ms ----------------------------------------------------------------------- for iterator | 0 ms | 0 ms | 0 ms | 2 ms ----------------------------------------------------------------------- for list.size() | 0 ms | 1 ms | 73 ms | 7972 ms ----------------------------------------------------------------------- for size = list.size() | 0 ms | 0 ms | 67 ms | 8216 ms ----------------------------------------------------------------------- for j-- | 0 ms | 1 ms | 67 ms | 8277 ms -----------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
compare loop performance of ArrayList ----------------------------------------------------------------------- list size | 10,000 | 100,000 | 1,000,000 | 10,000,000 ----------------------------------------------------------------------- for each | 1 ms | 3 ms | 14 ms | 152 ms ----------------------------------------------------------------------- for iterator | 0 ms | 1 ms | 12 ms | 114 ms ----------------------------------------------------------------------- for list.size() | 1 ms | 1 ms | 13 ms | 128 ms ----------------------------------------------------------------------- for size = list.size() | 0 ms | 0 ms | 6 ms | 62 ms ----------------------------------------------------------------------- for j-- | 0 ms | 1 ms | 6 ms | 63 ms -----------------------------------------------------------------------
compare loop performance of LinkedList ----------------------------------------------------------------------- list size | 100 | 1,000 | 10,000 | 100,000 ----------------------------------------------------------------------- for each | 0 ms | 1 ms | 1 ms | 2 ms ----------------------------------------------------------------------- for iterator | 0 ms | 0 ms | 0 ms | 2 ms ----------------------------------------------------------------------- for list.size() | 0 ms | 1 ms | 73 ms | 7972 ms ----------------------------------------------------------------------- for size = list.size() | 0 ms | 0 ms | 67 ms | 8216 ms ----------------------------------------------------------------------- for j-- | 0 ms | 1 ms | 67 ms | 8277 ms ----------------------------------------------------------------------- |
第一張表爲ArrayList對比結果,第二張表爲LinkedList對比結果。
表橫向爲同一遍歷方式不同大小list遍歷的時間消耗,縱向爲同一list不同遍歷方式遍歷的時間消耗。
PS:由於首次遍歷List會稍微多耗時一點,for each的結果稍微有點偏差,將測試代碼中的幾個Type順序調換會發現,for each耗時和for iterator接近。
3、遍歷方式性能測試結果分析
(1) foreach介紹
foreach是Java SE5.0引入的功能很強的循環結構,for (Integer j : list)應讀作for each int in list。
for (Integer j : list)實現幾乎等價於
Java
Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()) { Integer j = iterator.next(); }
1 2 3 4 |
Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()) { Integer j = iterator.next(); } |
下面的分析會將foreach和顯示調用集合迭代器兩種遍歷方式歸類爲Iterator方式,其他三種稱爲get方式遍歷。
這時我們已經發現foreach的一大好處,簡單一行實現了四行的功能,使得代碼簡潔美觀,另一大好處是相對於下標循環而言的,foreach不必關心下標初始值和終止值及越界等,所以不易出錯。Effective-Java中推薦使用此種寫法遍歷,本文會驗證這個說法。
使用foreach結構的類對象必須實現了Iterable接口,Java的Collection繼承自此接口,List實現了Collection,這個接口僅包含一個函數,源碼如下:
Java
package java.lang; import java.util.Iterator; /** * Implementing this interface allows an object to be the target of * the "foreach" statement. * * @param <T> the type of elements returned by the iterator * * @since 1.5 */ public interface Iterable<T> { /** * Returns an iterator over a set of elements of type T. * * @return an Iterator. */ Iterator<T> iterator(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package java.lang;
import java.util.Iterator;
/** * Implementing this interface allows an object to be the target of * the "foreach" statement. * * @param <T> the type of elements returned by the iterator * * @since 1.5 */ public interface Iterable<T> {
/** * Returns an iterator over a set of elements of type T. * * @return an Iterator. */ Iterator<T> iterator(); } |
iterator()用於返回一個Iterator,從foreach的等價實現中我們可以看到,會調用這個函數得到Iterator,再通過Iterator的next()得到下一個元素,hasNext()判斷是否還有更多元素。Iterator源碼如下:
Java
public interface Iterator<E> { boolean hasNext(); E next(); void remove(); }
1 2 3 4 5 6 7 |
public interface Iterator<E> { boolean hasNext();
E next();
void remove(); } |
(2) ArrayList遍歷方式結果分析
compare loop performance of ArrayList ----------------------------------------------------------------------- list size | 10,000 | 100,000 | 1,000,000 | 10,000,000 ----------------------------------------------------------------------- for each | 1 ms | 3 ms | 14 ms | 152 ms ----------------------------------------------------------------------- for iterator | 0 ms | 1 ms | 12 ms | 114 ms ----------------------------------------------------------------------- for list.size() | 1 ms | 1 ms | 13 ms | 128 ms ----------------------------------------------------------------------- for size = list.size() | 0 ms | 0 ms | 6 ms | 62 ms ----------------------------------------------------------------------- for j-- | 0 ms | 1 ms | 6 ms | 63 ms -----------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
compare loop performance of ArrayList ----------------------------------------------------------------------- list size | 10,000 | 100,000 | 1,000,000 | 10,000,000 ----------------------------------------------------------------------- for each | 1 ms | 3 ms | 14 ms | 152 ms ----------------------------------------------------------------------- for iterator | 0 ms | 1 ms | 12 ms | 114 ms ----------------------------------------------------------------------- for list.size() | 1 ms | 1 ms | 13 ms | 128 ms ----------------------------------------------------------------------- for size = list.size() | 0 ms | 0 ms | 6 ms | 62 ms ----------------------------------------------------------------------- for j-- | 0 ms | 1 ms | 6 ms | 63 ms ----------------------------------------------------------------------- |
PS:由於首次遍歷List會稍微多耗時一點,for each的結果稍微有點偏差,將測試代碼中的幾個Type順序調換會發現,for each耗時和for iterator接近。
從上面我們可以看出:
a. 在ArrayList大小爲十萬之前,五種遍歷方式時間消耗幾乎一樣
b. 在十萬以後,第四、五種遍歷方式快於前三種,get方式優於Iterator方式,並且
Java
int size = list.size(); for (int j = 0; j < size; j++) { list.get(j); }
1 2 3 4 |
int size = list.size(); for (int j = 0; j < size; j++) { list.get(j); } |
用臨時變量size取代list.size()性能更優。我們看看ArrayList中迭代器Iterator和get方法的實現
Java
private class Itr implements Iterator<E> { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount; public boolean hasNext() { return cursor != size; } @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } …… } public E get(int index) { rangeCheck(index); return elementData(index); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
private class Itr implements Iterator<E> { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount;
public boolean hasNext() { return cursor != size; }
@SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } …… }
public E get(int index) { rangeCheck(index);
return elementData(index); } |
從中可以看出get和Iterator的next函數同樣通過直接定位數據獲取元素,只是多了幾個判斷而已。
c . 從上可以看出即便在千萬大小的ArrayList中,幾種遍歷方式相差也不過50ms左右,且在常用的十萬左右時間幾乎相等,考慮foreach的優點,我們大可選用foreach這種簡便方式進行遍歷。
(3) LinkedList遍歷方式結果分析
compare loop performance of LinkedList ----------------------------------------------------------------------- list size | 100 | 1,000 | 10,000 | 100,000 ----------------------------------------------------------------------- for each | 0 ms | 1 ms | 1 ms | 2 ms ----------------------------------------------------------------------- for iterator | 0 ms | 0 ms | 0 ms | 2 ms ----------------------------------------------------------------------- for list.size() | 0 ms | 1 ms | 73 ms | 7972 ms ----------------------------------------------------------------------- for size = list.size() | 0 ms | 0 ms | 67 ms | 8216 ms ----------------------------------------------------------------------- for j-- | 0 ms | 1 ms | 67 ms | 8277 ms -----------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
compare loop performance of LinkedList ----------------------------------------------------------------------- list size | 100 | 1,000 | 10,000 | 100,000 ----------------------------------------------------------------------- for each | 0 ms | 1 ms | 1 ms | 2 ms ----------------------------------------------------------------------- for iterator | 0 ms | 0 ms | 0 ms | 2 ms ----------------------------------------------------------------------- for list.size() | 0 ms | 1 ms | 73 ms | 7972 ms ----------------------------------------------------------------------- for size = list.size() | 0 ms | 0 ms | 67 ms | 8216 ms ----------------------------------------------------------------------- for j-- | 0 ms | 1 ms | 67 ms | 8277 ms ----------------------------------------------------------------------- |
PS:由於首次遍歷List會稍微多耗時一點,for each的結果稍微有點偏差,將測試代碼中的幾個Type順序調換會發現,for each耗時和for iterator接近。
從上面我們可以看出:
a 在LinkedList大小接近一萬時,get方式和Iterator方式就已經差了差不多兩個數量級,十萬時Iterator方式性能已經遠勝於get方式。
我們看看LinkedList中迭代器和get方法的實現
Java
private class ListItr implements ListIterator<E> { private Node<E> lastReturned = null; private Node<E> next; private int nextIndex; private int expectedModCount = modCount; ListItr(int index) { // assert isPositionIndex(index); next = (index == size) ? null : node(index); nextIndex = index; } public boolean hasNext() { return nextIndex < size; } public E next() { checkForComodification(); if (!hasNext()) throw new NoSuchElementException(); lastReturned = next; next = next.next; nextIndex++; return lastReturned.item; } …… } public E get(int index) { checkElementIndex(index); return node(index).item; } /** * Returns the (non-null) Node at the specified element index. */ Node<E> node(int index) { // assert isElementIndex(index); if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
private class ListItr implements ListIterator<E> { private Node<E> lastReturned = null; private Node<E> next; private int nextIndex; private int expectedModCount = modCount;
ListItr(int index) { // assert isPositionIndex(index); next = (index == size) ? null : node(index); nextIndex = index; }
public boolean hasNext() { return nextIndex < size; }
public E next() { checkForComodification(); if (!hasNext()) throw new NoSuchElementException();
lastReturned = next; next = next.next; nextIndex++; return lastReturned.item; } …… }
public E get(int index) { checkElementIndex(index); return node(index).item; }
/** * Returns the (non-null) Node at the specified element index. */ Node<E> node(int index) { // assert isElementIndex(index);
if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } } |
從上面代碼中可以看出LinkedList迭代器的next函數只是通過next指針快速得到下一個元素並返回。而get方法會從頭遍歷直到index下標,查找一個元素時間複雜度爲哦O(n),遍歷的時間複雜度就達到了O(n2)。
所以對於LinkedList的遍歷推薦使用foreach,避免使用get方式遍歷。
(4) ArrayList和LinkedList遍歷方式結果對比分析
從上面的數量級來看,同樣是foreach循環遍歷,ArrayList和LinkedList時間差不多,可將本例稍作修改加大list size會發現兩者基本在一個數量級上。
但ArrayList get函數直接定位獲取的方式時間複雜度爲O(1),而LinkedList的get函數時間複雜度爲O(n)。
再結合考慮空間消耗的話,建議首選ArrayList。對於個別插入刪除非常多的可以使用LinkedList。
4、結論總結
通過上面的分析我們基本可以總結下:
(1) 無論ArrayList還是LinkedList,遍歷建議使用foreach,尤其是數據量較大時LinkedList避免使用get遍歷。
(2) List使用首選ArrayList。對於個別插入刪除非常多的可以使用LinkedList。
(3) 可能在遍歷List循環內部需要使用到下標,這時綜合考慮下是使用foreach和自增count還是get方式。