散列hash算法和加权随机java_WeightAlgorithm - 权重算法 - Random、HashCode对比

本文介绍了权重算法在分布式路由中的应用,并通过Java代码展示了使用Hash和Random两种方式实现加权随机选择的方法。通过对A、B、C三个选项进行500000次选择,对比了两种方法的执行时间和结果分布。
摘要由CSDN通过智能技术生成

一、权重算法

权重算法一般在路由里面用的比较多,分布式环境下对等的服务有多个,加权随机选出一个服务来调用;

可能还有其他方面的用途,下面的代码简单的实现了这个权重,本质上就用到了数组,随机下标;

二、代码概览

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 hashRes = getList4Hash(weight, times);

printRes("Hash", hashStart, hashRes);

final long randomStart = System.currentTimeMillis();

List 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%

}

三、完整代码

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 hashRes = getList4Hash(weight, times);

printRes("Hash", hashStart, hashRes);

final long randomStart = System.currentTimeMillis();

List 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 resList) {

final Map 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 getList4Hash(String[] weight, int times) {

List 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 getList4Random(String[] weight, int times) {

List 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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值