zoukankan      html  css  js  c++  java
  • 序列号生成的另一种玩法

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

    1,存放时占用空间大,

    2,查询时效率慢

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

    我们知道:

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

    ASCII 码 字符   ASCII 码 字符   ASCII 码 字符   ASCII 码 字符
    十进位 十六进位   十进位 十六进位   十进位 十六进位   十进位 十六进位
    032 20     056 38 8   080 50 P   104 68 h
    033 21 !   057 39 9   081 51 Q   105 69 i
    034 22 "   058 3A :   082 52 R   106 6A j
    035 23 #   059 3B ;   083 53 S   107 6B k
    036 24 $   060 3C <   084 54 T   108 6C l
    037 25 %   061 3D =   085 55 U   109 6D m
    038 26 &   062 3E >   086 56 V   110 6E n
    039 27 '   063 3F ?   087 57 W   111 6F o
    040 28 (   064 40 @   088 58 X   112 70 p
    041 29 )   065 41 A   089 59 Y   113 71 q
    042 2A *   066 42 B   090 5A Z   114 72 r
    043 2B +   067 43 C   091 5B [   115 73 s
    044 2C ,   068 44 D   092 5C   116 74 t
    045 2D -   069 45 E   093 5D ]   117 75 u
    046 2E .   070 46 F   094 5E ^   118 76 v
    047 2F /   071 47 G   095 5F _   119 77 w
    048 30 0   072 48 H   096 60 `   120 78 x
    049 31 1   073 49 I   097 61 a   121 79 y
    050 32 2   074 4A J   098 62 b   122 7A z
    051 33 3   075 4B K   099 63 c   123 7B {
    052 34 4   076 4C L   100 64 d   124 7C |
    053 35 5   077 4D M   101 65 e   125 7D }
    054 36 6   078 4E N   102 66 f   126 7E ~
    055 37 7   079 4F O   103 67 g   127 7F DEL

    小写字符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%的长度,当前查询效率也得到相应的提升了。

    是不是蛮有趣的?

  • 相关阅读:
    实验四Web服务器2
    发际线与我作队团队作业(五):冲刺总结1
    读书笔记
    socket3
    使用CAS来实现个单例模式
    基于无锁的C#并发队列实现
    C#3.0中的“多重继承”
    对并发/并行编程的总结
    谈谈多线程编程(二) 不变对象
    谈谈多线程编程(一) 安全性策略
  • 原文地址:https://www.cnblogs.com/davidwang456/p/8441259.html
Copyright © 2011-2022 走看看