WeightAlgorithm - 權(quán)重算法 - Random、HashCode對(duì)比

From GitHub Page:https://hisen.me/20190322-WeightAlgorithm%20-%20%E6%9D%83%E9%87%8D%E7%AE%97%E6%B3%95%20-%20Random%E3%80%81HashCode%E5%AF%B9%E6%AF%94/#more

一、權(quán)重算法

權(quán)重算法一般在路由里面用的比較多,分布式環(huán)境下對(duì)等的服務(wù)有多個(gè),加權(quán)隨機(jī)選出一個(gè)服務(wù)來(lái)調(diào)用;

可能還有其他方面的用途,下面的代碼簡(jiǎn)單的實(shí)現(xiàn)了這個(gè)權(quán)重,本質(zhì)上就用到了數(shù)組,隨機(jī)下標(biāo);

二、代碼概覽

public static void main(String[] args) {
    String[] weight = {"A", "A", "A", "A", "A", "B", "B", "B", "C", "C"};
    final int times = 500000;
    final long hashStart = System.currentTimeMillis();
    List<String> hashRes = getList4Hash(weight, times);
    printRes("Hash", hashStart, hashRes);

    final long randomStart = System.currentTimeMillis();
    List<String> randomRes = getList4Random(weight, times);
    printRes("Random", randomStart, randomRes);
//        Hash          use millis: 931
//        A:49.92%
//        B:29.99%
//        C:20.09%
//        Random        use millis: 50
//        A:50.08%
//        B:29.93%
//        C:19.99%
}

三、完整代碼

github:https://github.com/hisenyuan/IDEAPractice/blob/master/src/main/java/com/hisen/algorithms/WeightAlgorithm.java

package com.hisen.algorithms;

import com.google.common.collect.Lists;

import java.text.NumberFormat;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author hisenyuan
 * @Description $end$
 * @Date 2019/3/21 21:04
 */
public class WeightAlgorithm {
    public static void main(String[] args) {
        String[] weight = {"A", "A", "A", "A", "A", "B", "B", "B", "C", "C"};
        final int times = 500000;
        final long hashStart = System.currentTimeMillis();
        List<String> hashRes = getList4Hash(weight, times);
        printRes("Hash", hashStart, hashRes);

        final long randomStart = System.currentTimeMillis();
        List<String> randomRes = getList4Random(weight, times);
        printRes("Random", randomStart, randomRes);
//        Hash          use millis: 931
//        A:49.92%
//        B:29.99%
//        C:20.09%
//        Random        use millis: 50
//        A:50.08%
//        B:29.93%
//        C:19.99%
    }

    private static void printRes(String method, long start, List<String> resList) {
        final Map<String, Long> collect = resList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        final double a = collect.get("A");
        final double b = collect.get("B");
        final double c = collect.get("C");
        final double sum = a + b + c;

        NumberFormat nt = NumberFormat.getPercentInstance();
        nt.setMinimumFractionDigits(2);

        System.out.println(method + "\t use millis: " + (System.currentTimeMillis() - start));
        System.out.println("A" + ":" + nt.format(a / sum));
        System.out.println("B" + ":" + nt.format(b / sum));
        System.out.println("C" + ":" + nt.format(c / sum));
    }

    private static List<String> getList4Hash(String[] weight, int times) {
        List<String> result = Lists.newArrayList();
        for (int i = 0; i < times; i++) {
            final String a = UUID.randomUUID().toString() + System.currentTimeMillis();
            final int hash = a.hashCode();
            final int index = hash > 0 ? hash : -hash;
            final String res = weight[index % weight.length];
            result.add(res);
        }
        return result;
    }

    private static List<String> getList4Random(String[] weight, int times) {
        List<String> result = Lists.newArrayList();
        Random random = new Random();
        for (int i = 0; i < times; i++) {
            final int index = random.nextInt(weight.length);
            final String res = weight[index];
            result.add(res);
        }
        return result;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容