Another way to play serial number generation

Usually, we like to use time as a kind of serial number when generating serial number, but the length of time serial number is 15, which leads to our serial number being very long

1. Large storage space,

2. Slow query efficiency

Can we shorten the time series number?

We know:

According to the ASCII coding table, we can see that

< table class = "gen" border = "1" cellspacing = "0" cellpadding = "0" > < tr > < TD class = "field" colSpan = "2" > ASCII < / td > < TD class = "field" rowspan = "2" > characters < / td > < TD class = "BK" > < / td > < TD class = "field" colSpan = "2" > ASCII < / td > < TD class = "field" rowspan = "2" > characters < / td > < TD class = "BK" > < / td > < TD class = "field" colSpan = "2" > ASCII < / td > < TD class = "field" Rowspan = "2" > character < / td > < TD class = "BK" > < / td > < TD class = "field" colSpan = "2" > ASCII code < / td > < TD class = "field" rowspan = "2" > character < / td > < tr > < TD class = "field" > decimal < / td > < TD class = "field" > hexadecimal < / td > < TD class = "BK" > < / td > < TD class = "field" > decimal < / td > < TD class = "field" > hexadecimal < / td > < TD class = "BK" > < / td > < TD class = "field" >Decimal < / td > < TD class = "field" > hexadecimal < / td > < TD class = "BK" > < / td > < TD class = "field" > decimal < / td > < TD class = "field" > hexadecimal < / td > < tr > < TD class = "cellgen" > 032 < / td > < TD class = "cellgen" > 20 < / td > < TD class = "cellgen" > < / td > < TD class = "BK" > < / td > < TD class = "cellgen" > 056 < / td > < TD class = "cellgen" > 38 < / td > < TD class = "cellgen" >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

Encoding length of lowercase character a (97) when using different storage

Binary: 0110001

Octal: 141

Decimal: 97

Hex: 61

It can be seen that with the increase of base, the length of characters will become shorter and shorter. If we take the 62 characters of 0123456789 abcdefghijklmnopqrstuvwxyz we commonly use as the code, then 62 base can be represented.

Before coding again, I searched GIT and found that there is already a code () on it. I won't implement it again. The code is as follows:

1. Encoding, converting long type to binary string

2. Decoding, inverse process

Test case:

The console output results are as follows:

The length has been changed from 15 bits to 9 bits, reducing the length by 40%, and the current query efficiency has been improved accordingly.

Isn't it interesting?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>