java 获取验证码优化(使用字符串截取,修改整数运算,提升n倍)

int count = 10000;
        long start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            String code = (Math.random() + "").substring(2, 8);
            if (code.length() != 6) {
                System.out.println("生成失败:" + code);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("截取耗时:" + (end - start));

        System.out.println("===================================================");
        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            String code = String.valueOf((int) ((Math.random() * 9 + 1) * Math.pow(10, 5)));
            if (code.length() != 6) {
                System.out.println("生成失败:" + code);
            }
        }
        end = System.currentTimeMillis();
        System.out.println("整数运算,执行时间:" + (end - start));

耗时:java 获取验证码优化(使用字符串截取,修改整数运算,提升n倍)_第1张图片

 

你可能感兴趣的:(java,jvm,数据结构)