欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中 shuffle 算法的使用

 更新時(shí)間:2013年04月15日 09:57:33   作者:  
本篇文章,小編將為大家介紹,在Java中 shuffle 算法的使用,有需要的朋友可以參考一下

Fisher–Yates shuffle 基本思想(Knuth shuffle ):

To shuffle an array a of n elements (indices 0..n-1):
  for i from n − 1 downto 1 do
       j ← random integer with 0 ≤ j ≤ i
       exchange a[j] and a[i]

JDK源代碼如下:

復(fù)制代碼 代碼如下:

/**
     * Moves every element of the List to a random new position in the list.
     *
     * @param list
     *            the List to shuffle
     *
     * @throws UnsupportedOperationException
     *             when replacing an element in the List is not supported
     */
    public static void shuffle(List<?> list) {
        shuffle(list, new Random());
    }

    /**
     * Moves every element of the List to a random new position in the list
     * using the specified random number generator.
     *
     * @param list
     *            the List to shuffle
     * @param random
     *            the random number generator
     *
     * @throws UnsupportedOperationException
     *             when replacing an element in the List is not supported
     */
    @SuppressWarnings("unchecked")
    public static void shuffle(List<?> list, Random random) {
        if (!(list instanceof RandomAccess)) {
            Object[] array = list.toArray();
            for (int i = array.length - 1; i > 0; i--) {
                int index = random.nextInt(i + 1);
                if (index < 0) {
                    index = -index;
                }
                Object temp = array[i];
                array[i] = array[index];
                array[index] = temp;
            }

            int i = 0;
            ListIterator<Object> it = (ListIterator<Object>) list
                    .listIterator();
            while (it.hasNext()) {
                it.next();
                it.set(array[i++]);
            }
        } else {
            List<Object> rawList = (List<Object>) list;
            for (int i = rawList.size() - 1; i > 0; i--) {
                int index = random.nextInt(i + 1);
                if (index < 0) {
                    index = -index;
                }
                rawList.set(index, rawList.set(i, rawList.get(index)));
            }
        }
    }


測(cè)試代碼,為了確保每種情況的初始化一樣,使用了多個(gè)容器:
復(fù)制代碼 代碼如下:

public class javaShuffle {
    public static int temp = 0;
    public static long start;
    public static long end;

    public static void main(final String args[]) {

        Object changeTemp;
        List<Integer> numList = new ArrayList<Integer>();
        List<Integer> firstList = new ArrayList<Integer>();
        List<Integer> secondList = new ArrayList<Integer>();
        List<Integer> thirdList = new ArrayList<Integer>();
        List<Integer> fourthList = new ArrayList<Integer>();

        for (int i = 1; i <= 100000; i++) {
            numList.add(i);
            firstList.add(i);
            secondList.add(i);
            thirdList.add(i);
            fourthList.add(i);
        }

        // first shuffle,use changeTemp
        getStartTime();
        int randInt = 0;
        for (int i = 0, length = firstList.size(); i < length; i++) {
            randInt = getRandom(i, firstList.size());
            changeTemp = firstList.get(i);
            firstList.set(i, firstList.get(randInt));
            firstList.set(randInt, javaShuffle.temp);
        }
        getEndTime("first shuffle run time ");

        // second shuffle,exchange list
        getStartTime();
        for (int i = 0, length = secondList.size(); i < length; i++) {
            randInt = getRandom(i, secondList.size());
            secondList.set(i, secondList.set(randInt, secondList.get(i)));
        }
        getEndTime("second shuffle run time");

        // third shuffle, change generate random int
        getStartTime();
        Object[] tempArray = thirdList.toArray();
        Random rand = new Random();
        int j = 0;
        for (int i = tempArray.length - 1; i > 0; i--) {
            j = rand.nextInt(i + 1);
            thirdList.set(i, thirdList.set(j, thirdList.get(i)));
        }

        getEndTime("third shuffle run time ");

        // fourth shuffle, simulate java shuffle
        getStartTime();
        Random random = new Random();
        if (!(fourthList instanceof RandomAccess)) {
            Object[] array = fourthList.toArray();
            for (int i = array.length - 1; i > 0; i--) {
                int index = random.nextInt(i + 1);
                if (index < 0) {
                    index = -index;
                }
                Object temp = array[i];
                array[i] = array[index];
                array[index] = temp;
            }

            int i = 0;
            ListIterator<Integer> it = (ListIterator<Integer>) fourthList.listIterator();
            while (it.hasNext()) {
                it.next();
                it.set((Integer) array[i++]);
            }
        } else {
            List<Integer> rawList = (List<Integer>) fourthList;
            for (int i = rawList.size() - 1; i > 0; i--) {
                int index = random.nextInt(i + 1);
                if (index < 0) {
                    index = -index;
                }
                rawList.set(index, rawList.set(i, rawList.get(index)));
            }
        }
        getEndTime("fourth shuffle run time");

        // java shuffle
        getStartTime();
        Collections.shuffle(numList);
        getEndTime("java shuffle run time  ");
    }

    public static void swap(int a, int b) {
        javaShuffle.temp = a;
        a = b;
        b = javaShuffle.temp;
    }

    public static int getRandom(final int low, final int high) {
        return (int) (Math.random() * (high - low) + low);
    }

    public static void getStartTime() {
        javaShuffle.start = System.nanoTime();
    }

    public static void getEndTime(final String s) {
        javaShuffle.end = System.nanoTime();
        System.out.println(s + ": " + (javaShuffle.end - javaShuffle.start) + "ns");
    }

}

如果數(shù)值較小,例如100000級(jí)別,則輸出大概是:

first shuffle run time : 85029499ns
second shuffle run time: 80909474ns
third shuffle run time : 71543926ns
fourth shuffle run time: 76520595ns
java shuffle run time  : 61027643ns

first shuffle run time : 82326239ns
second shuffle run time: 78575611ns
third shuffle run time : 95009632ns
fourth shuffle run time: 105946897ns
java shuffle run time  : 90849302ns

first shuffle run time : 84539840ns
second shuffle run time: 85965575ns
third shuffle run time : 101814998ns
fourth shuffle run time: 113309672ns
java shuffle run time  : 35089693ns

first shuffle run time : 87679863ns
second shuffle run time: 79991814ns
third shuffle run time : 73720515ns
fourth shuffle run time: 78353061ns
java shuffle run time  : 64146465ns

first shuffle run time : 84314386ns
second shuffle run time: 80074803ns
third shuffle run time : 74001283ns
fourth shuffle run time: 79931321ns
java shuffle run time  : 86427540ns

first shuffle run time : 84315523ns
second shuffle run time: 81468386ns
third shuffle run time : 75052284ns
fourth shuffle run time: 79461407ns
java shuffle run time  : 66607729ns


多次運(yùn)行結(jié)果可能都不一樣,但是基本java自帶 shuffle速度最快,第三種方式次之。而第一種方法耗時(shí)最久。

如果是10000000級(jí)別,大概如下:

first shuffle run time : 2115703288ns
second shuffle run time: 3114045871ns
third shuffle run time : 4664426798ns
fourth shuffle run time: 2962686695ns
java shuffle run time  : 3246883026ns first shuffle run time : 2165398466ns
second shuffle run time: 3129558913ns
third shuffle run time : 4147859664ns
fourth shuffle run time: 2911849942ns
java shuffle run time  : 4311703487ns first shuffle run time : 2227462247ns
second shuffle run time: 3279548770ns
third shuffle run time : 4704344954ns
fourth shuffle run time: 2942635980ns
java shuffle run time  : 3933172427ns first shuffle run time : 2200158789ns
second shuffle run time: 3172666791ns
third shuffle run time : 4715631517ns
fourth shuffle run time: 2950817535ns
java shuffle run time  : 3387417676ns first shuffle run time : 2201124449ns
second shuffle run time: 3203823874ns
third shuffle run time : 4179926278ns
fourth shuffle run time: 2913690411ns
java shuffle run time  : 3571313813ns first shuffle run time : 2163053190ns
second shuffle run time: 3073889926ns
third shuffle run time : 4493831518ns
fourth shuffle run time: 2852713887ns
java shuffle run time  : 3773602415ns

可以看出,第一種方法速度最快,而第四種最慢。java自帶 shuffle速度也不理想。

在進(jìn)行大數(shù)據(jù)處理的時(shí)候,如果使用java庫(kù)效率較低時(shí),可以考慮使用其他方式。

 

相關(guān)文章

  • springboot項(xiàng)目獲取resources相對(duì)路徑的方法

    springboot項(xiàng)目獲取resources相對(duì)路徑的方法

    這篇文章主要介紹了springboot項(xiàng)目獲取resources相對(duì)路徑的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • SpringBoot快速構(gòu)建應(yīng)用程序方法介紹

    SpringBoot快速構(gòu)建應(yīng)用程序方法介紹

    這篇文章主要介紹了SpringBoot快速構(gòu)建應(yīng)用程序方法介紹,涉及SpringBoot默認(rèn)的錯(cuò)誤頁(yè)面,嵌入式Web容器層面的約定和定制等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-11-11
  • 打造一款代碼命名工具的詳細(xì)教程

    打造一款代碼命名工具的詳細(xì)教程

    這篇文章主要介紹了來(lái),我們一起打造一款代碼命名工具,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • java如何實(shí)現(xiàn)字符串中的字母排序

    java如何實(shí)現(xiàn)字符串中的字母排序

    這篇文章主要介紹了java如何實(shí)現(xiàn)字符串中的字母排序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • MyBatis Log 插件無(wú)法顯示SQL語(yǔ)句的原因解析

    MyBatis Log 插件無(wú)法顯示SQL語(yǔ)句的原因解析

    MyBatis Log是IDEA一款下載量非常高的插件,該插件可以對(duì)控制臺(tái)打印的日志進(jìn)行解析,然后將對(duì)應(yīng)的SQL語(yǔ)句整理并拼接好對(duì)應(yīng)的參數(shù),非常方便。這篇文章給大家介紹MyBatis Log 插件無(wú)法顯示SQL語(yǔ)句的原因,感興趣的朋友跟隨小編一起看看吧
    2020-09-09
  • idea2020安裝MybatisCodeHelper插件的圖文教程

    idea2020安裝MybatisCodeHelper插件的圖文教程

    這篇文章主要介紹了idea2020安裝MybatisCodeHelper插件的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java連接六類數(shù)據(jù)庫(kù)技巧全攻略

    Java連接六類數(shù)據(jù)庫(kù)技巧全攻略

    本文主要為大家介紹了Java與Oracle、DB2、Sql Server、Sybase、MySQL、PostgreSQL等數(shù)據(jù)庫(kù)連接的方法。
    2015-09-09
  • Java中finally關(guān)鍵字對(duì)返回值的影響詳解

    Java中finally關(guān)鍵字對(duì)返回值的影響詳解

    這篇文章主要介紹了Java中finally關(guān)鍵字對(duì)返回值的影響詳解,執(zhí)行完try catch里面內(nèi)容準(zhǔn)備return時(shí),如果還有finally需要執(zhí)行這是編譯器會(huì)為我們?cè)黾右粋€(gè)全局變量去暫存return 的值,等到finally執(zhí)行完成去return這個(gè)全局變量,需要的朋友可以參考下
    2024-01-01
  • Java兩種常用的隨機(jī)數(shù)生成方式(小白總結(jié))

    Java兩種常用的隨機(jī)數(shù)生成方式(小白總結(jié))

    這篇文章主要介紹了Java兩種常用的隨機(jī)數(shù)生成方式(小白總結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • mybatis對(duì)于list更新sql語(yǔ)句的寫(xiě)法說(shuō)明

    mybatis對(duì)于list更新sql語(yǔ)句的寫(xiě)法說(shuō)明

    這篇文章主要介紹了mybatis對(duì)于list更新sql語(yǔ)句的寫(xiě)法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評(píng)論