分析輪子(七)- RandomAccess.java

 

1:仍是先上一個類的繼承關係比較圖吧!html

2:看一下 RandomAccess.java 的源碼,空空如也,什麼都沒有,那她有什麼用處呢?java

/**
 * Marker interface used by <tt>List</tt> implementations to indicate that
 * they support fast (generally constant time) random access.  The primary
 * purpose of this interface is to allow generic algorithms to alter their
 * behavior to provide good performance when applied to either random or
 * sequential access lists.
 *
 * <p>The best algorithms for manipulating random access lists (such as
 * <tt>ArrayList</tt>) can produce quadratic behavior when applied to
 * sequential access lists (such as <tt>LinkedList</tt>).  Generic list
 * algorithms are encouraged to check whether the given list is an
 * <tt>instanceof</tt> this interface before applying an algorithm that would
 * provide poor performance if it were applied to a sequential access list,
 * and to alter their behavior if necessary to guarantee acceptable
 * performance.
 *
 * <p>It is recognized that the distinction between random and sequential
 * access is often fuzzy.  For example, some <tt>List</tt> implementations
 * provide asymptotically linear access times if they get huge, but constant
 * access times in practice.  Such a <tt>List</tt> implementation
 * should generally implement this interface.  As a rule of thumb, a
 * <tt>List</tt> implementation should implement this interface if,
 * for typical instances of the class, this loop:
 * <pre>
 *     for (int i=0, n=list.size(); i &lt; n; i++)
 *         list.get(i);
 * </pre>
 * runs faster than this loop:
 * <pre>
 *     for (Iterator i=list.iterator(); i.hasNext(); )
 *         i.next();
 * </pre>
 *
 * <p>This interface is a member of the
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
 * Java Collections Framework</a>.
 *
 * @since 1.4
 */
public interface RandomAccess {
}

有點磕磕巴巴,閱讀了源碼中的註釋,大概講解了一下 RandomAccess.java 接口的做用,它是一個標記接口,表示實現它的類支持快速隨機訪問,經過上圖的對比咱們看到實現List接口的類,有些是支持快速隨機訪問的,有些不支持,怎麼標記哪?就是用 RandomAccess.java 接口來標記,這樣有什麼好處呢?能夠在通用的實現List集合遍歷的時候算法中,針對實現 RandomAccess.java 接口的集合,能夠有選擇性的使用性能更好的遍歷方式,我也實驗了一把,繼續往下看吧!算法

3:簡單的集合遍歷性能對比小栗子,代碼比較簡單,能夠調整參數自行玩一玩app

/**
 * @description:測試循環方式的性能
 * @author:godtrue
 * @create:2018-09-11
 */
public class TestTraverse {
    /**
     * 開始循環的基值
     */
    private static final int START_LOOP = 1;

    /**
     * 結束循環的基值
     * 個人機器 1億 次就卡死了,我就實驗下 1千萬 次吧!
     */
    private static final int END_LOOP = 10000000;

    /**
    *
    *@description: 測試入口,主方法
    *@param args
    *@return: void
    *@author: godtrue
    *@createTime: 2018-09-11
    *@version: v1.0
    */
    public static void main(String[] args) {
        /**
         * 注意:
         * 1:測試時,一個個來跑,避免相互影響
         * 2:能夠逐漸,將 TestTraverse.END_LOOP 調高,用於觀察不一樣量級的循環的運行結果
         */
        traverse(genArrayList());
        //traverse(genLinkedList());
    }

    /**
    *
    *@description: 遍歷 list 集合,這裏能體現到 RandomAccess 接口的做用,能夠選擇不一樣的遍歷集合的方式
    *@param list
    *@return: void
    *@author: godtrue
    *@createTime: 2018-09-11
    *@version: v1.0
    */
    private static void traverse(List list){
        if(list instanceof RandomAccess){
            traverseByLoop(list);
        }else {
            traverseByIterator(list);
        }
    }

    /**
    *
    *@description: 循環遍歷 list 集合
    *@param list
    *@return: void
    *@author: godtrue
    *@createTime: 2018-09-11
    *@version: v1.0
    */
    private static void traverseByLoop(List list){
        long startTime = System.currentTimeMillis();
        for(int i=0,sum=list.size();i<sum;i++){
            list.get(i);
        }
        System.out.println("exe traverseByLoop cost time : "+(System.currentTimeMillis()-startTime));
    }

    /**
    *
    *@description: 迭代遍歷 list 集合
    *@param list
    *@return: void
    *@author: godtrue
    *@createTime: 2018-09-11
    *@version: v1.0
    */
    private static void traverseByIterator(List list){
        long startTime = System.currentTimeMillis();
          for (Iterator i=list.iterator(); i.hasNext(); ){
              i.next();
          }
        System.out.println("exe traverseByIterator cost time : "+(System.currentTimeMillis()-startTime));
    }

    /**
    *
    *@description:  生成 ArrayList 數據信息
    *@param
    *@return: java.util.List<java.lang.String>
    *@author: godtrue
    *@createTime: 2018-09-11
    *@version: v1.0
    */
    private static List<String> genArrayList(){
        long startTime = System.currentTimeMillis();
        List<String> list = new ArrayList<String>();
        for(int i=TestTraverse.START_LOOP;i<TestTraverse.END_LOOP;i++){
            list.add(String.valueOf(i));
        }
        System.out.println("exe genArrayList cost time : "+(System.currentTimeMillis()-startTime));
        return list;
    }

    /**
    *
    *@description: 生成 LinkedList 數據信息
    *@param
    *@return: java.util.List<java.lang.String>
    *@author: godtrue
    *@createTime: 2018-09-11
    *@version: v1.0
    */
    private static List<String> genLinkedList(){
        long startTime = System.currentTimeMillis();
        List<String> list = new LinkedList<String>();
        for(int i=TestTraverse.START_LOOP;i<TestTraverse.END_LOOP;i++){
            list.add(String.valueOf(i));
        }
        System.out.println("exe genLinkedList cost time : "+(System.currentTimeMillis()-startTime));
        return list;
    }
}

4:下面是實驗環境的信息和結果dom

4-1)實驗的硬件信息ide

4-2)運行時的性能指標參數oop

4-3)迭代遍歷的運行狀況性能

4-4)隨機遍歷的運行狀況,對比一下,能夠看出性能相差的仍是蠻多的測試

相關文章
相關標籤/搜索