序列号生成的另一种玩法

通常我们生成序列号的时候喜欢把时间作为序列号的一种,但时间序列号的长度为15,这样导致我们的序列号就非常长导致

1,存放时占用空间大,

2,查询时效率慢

我们是不是可以把时间序列号变短呢?

我们知道:

根据ascII编码表,我们可以看出

 

 

ASCII 码字符 ASCII 码字符 ASCII 码字符 ASCII 码字符
十进位十六进位 十进位十六进位 十进位十六进位 十进位十六进位
03220  056388 08050P 10468h
03321! 057399 08151Q 10569i
03422" 0583A: 08252R 1066Aj
03523# 0593B; 08353S 1076Bk
03624$ 0603C< 08454T 1086Cl
03725% 0613D= 08555U 1096Dm
03826& 0623E> 08656V 1106En
03927' 0633F? 08757W 1116Fo
04028( 06440@ 08858X 11270p
04129) 06541A 08959Y 11371q
0422A* 06642B 0905AZ 11472r
0432B+ 06743C 0915B[ 11573s
0442C, 06844D 0925C\ 11674t
0452D- 06945E 0935D] 11775u
0462E. 07046F 0945E^ 11876v
0472F/ 07147G 0955F_ 11977w
048300 07248H 09660` 12078x
049311 07349I 09761a 12179y
050322 0744AJ 09862b 1227Az
051333 0754BK 09963c 1237B{
052344 0764CL 10064d 1247C|
053355 0774DM 10165e 1257D}
054366 0784EN 10266f 1267E~
055377 0794FO 10367g 1277FDEL

小写字符a(97) 使用不同存储时的编码长度

二进制:01100001

八进制:141

十进制:97

十六进制:61

可以看出,随着进制的增高,字符的长度也会越来越短,如果我们拿我们常用的0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ这个62个字符作为编码,那么62进制就可以表示了。

再进行编码前,我搜了一下git,上面已经有代码实现了(base62),我就不再实现一遍了,代码如下:

1.编码,将long型转换为62进制字符串

    /**
     * Encodes a decimal value to a Base62 <code>String</code>.
     * 
     * @param b10
     *            the decimal value to encode, must be nonnegative.
     * @return the number encoded as a Base62 <code>String</code>.
     */
    public String encodeBase10(long b10) {
        if (b10 < 0) {
            throw new IllegalArgumentException("b10 must be nonnegative");
        }
        String ret = "";
        while (b10 > 0) {
            ret = characters.charAt((int) (b10 % 62)) + ret;
            b10 /= 62;
        }
        return ret;

    }

2.解码,逆过程

    /**
     * Decodes a Base62 <code>String</code> returning a <code>long</code>.
     * 
     * @param b62
     *            the Base62 <code>String</code> to decode.
     * @return the decoded number as a <code>long</code>.
     * @throws IllegalArgumentException
     *             if the given <code>String</code> contains characters not
     *             specified in the constructor.
     */
    public long decodeBase62(String b62) {
        for (char character : b62.toCharArray()) {
            if (!characters.contains(String.valueOf(character))) {
                throw new IllegalArgumentException("Invalid character(s) in string: " + character);
            }
        }
        long ret = 0;
        b62 = new StringBuffer(b62).reverse().toString();
        long count = 1;
        for (char character : b62.toCharArray()) {
            ret += characters.indexOf(character) * count;
            count *= 62;
        }
        return ret;
    }

测试用例:

    public static void main(String[] args) {
        Base62 encoder=new Base62();
        Long time=System.nanoTime();
        String timeStr=encoder.encodeBase10(time);
        System.out.println(timeStr);
        
        System.out.println(time);
        System.out.println(encoder.decodeBase62(timeStr));
    }

console输出结果如下:

2OdCqJOH8
613534552694770
613534552694770

长度由15位变为9位,减少了40%的长度,当前查询效率也得到相应的提升了。

是不是蛮有趣的?

 

转载于:https://www.cnblogs.com/davidwang456/p/8441259.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值