哈希碰撞率(hash collision)实测

样本数1万10万100万1000万1亿
碰撞率10.00000002  0.00000033  0.000037690.00363519 0.29502696 
碰撞率20.000000010.000000510.000038080.003643750.2949834
碰撞率30.000000010.00000050.000038180.003637530.29490623

测试代码如下:


import cn.hutool.core.codec.Base64;
import cn.hutool.core.math.MathUtil;
import cn.hutool.core.util.StrUtil;

import java.math.BigDecimal;
import java.text.DecimalFormat;


public class HashCollision {
    public static void main(String[] args) {
        test();
        test();
        test();
    }

    private static void test(){
        System.out.println("test");
        long nums = 100000000;
        long s = System.currentTimeMillis();
        BitMap bitMap = new BitMap();
        long collisionNum = 0;
        for (int i = 0; i < nums; i++) {
            String uid = getUID();
            int code = uid.hashCode();
            if (bitMap.put(code)) {
                collisionNum++;
            }
            if (i+1 == 10000 || i+1 == 100000 || i+1 == 1000000 || i+1 == 10000000 || i+1 == 100000000) {

                DecimalFormat df = new DecimalFormat();
                df.setMaximumFractionDigits(10);
                String rate = df.format(collisionNum / (0.0 + nums));
//                System.out.println("collision  rate "+(i+1)+" = " + rate);
                System.out.print(rate+"\t");
            }
        }
        System.out.println();
        long t = System.currentTimeMillis() - s;
        System.out.println("t = " + t);
    }

    public static String getUID() {
        long[] longs = new long[]{
                Double.doubleToLongBits(Math.random()),
                Double.doubleToLongBits(Math.random()),
                Double.doubleToLongBits(Math.random())
        };
        byte[] bytes = longArrayToByteArray(longs);
        String encode = Base64.encode(bytes);
        return encode;
    }

    public static byte[] longArrayToByteArray(long[] longs) {
        byte[] bytes = new byte[longs.length * 8];
        for (int i = 0; i < longs.length; i++) {
            for (int j = 0; j < 8; j++) {
                bytes[i * 8 + j] = (byte) (longs[i] >>> j);
            }
        }
        return bytes;
    }

}

public class BitMap {
    byte[] data = new byte[0x20000000];

    public boolean put(int code) {
        int indexInArray=code >>> 8;
        byte byteInArray = data[indexInArray];
        int indexInByte = code & 7;
        int bitValue = byteInArray >>> indexInByte & 1;
        data[indexInArray] = (byte) (byteInArray | (1 << indexInByte));
        return bitValue == 1;
    }

    public void clear(){
        for (int i = 0; i < data.length; i++) {
            data[i]=0;
        }
    }
    public static void main(String[] args) {
        BitMap bitMap = new BitMap();
        int code="111".hashCode();
        boolean b = bitMap.put(code);
        System.out.println("b = " + b);
        b = bitMap.put(code);
        System.out.println("b = " + b);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值