liurunyu
2024-12-12 db8bd443faa40789e3b82a75ccebeb744d55a5ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.dy.common.util;
 
/**
 * MurmurHash算法:高运算性能,低碰撞率,由Austin Appleby创建于2008年,
 * 现已应用到Hadoop、libstdc++、nginx、libmemcached等开源系统。2011年
 * Appleby被Google雇佣,随后Google推出其变种的CityHash算法。 
 * 
 * 官方网站:https://sites.google.com/site/murmurhash/ 
 * 
 * MurmurHash算法,自称超级快的hash算法,是FNV的4-5倍。官方数据如下: 
 * 
 * OneAtATime – 354.163715 mb/sec 
 * FNV – 443.668038 mb/sec 
 * SuperFastHash – 985.335173 mb/sec 
 * lookup3 – 988.080652 mb/sec 
 * MurmurHash 1.0 – 1363.293480 mb/sec 
 * MurmurHash 2.0 – 2056.885653 mb/sec 
 * 
 * 但也有文章声称,只有当key的长度大于10字节的时候,MurmurHash的运算速
 * 度才快于DJB。从计算速度上来看,MurmurHash只适用于已知长度的、长度比
 * 较长的字符。 
 * 
 * 哈希值分布非常均匀,即低碰撞率
 * 比Crc16更为均匀
 */
public final class MurmurHash {  
      
    private byte[] toBytesWithoutEncoding(String str) {  
        int len = str.length();  
        int pos = 0;  
        byte[] buf = new byte[len << 1];  
        for (int i = 0; i < len; i++) {  
            char c = str.charAt(i);  
            buf[pos++] = (byte) (c & 0xFF);  
            buf[pos++] = (byte) (c >> 8);  
        }  
        return buf;  
    }  
    
    /**
     * 刘润玉增加方法于2016-12-02
     * @param data
     * @param length
     * @return
     */
    public int hash16_plus(final byte[] data, int length) {  
        int hash = hash32(data, length, 0x9747b28c);
        hash = hash % 65535 ;
        if(hash < 0){
            hash = - hash ;
        }
        return hash ;
    }  
    /**
     * 刘润玉增加方法于2016-12-02  
     * @param data
     * @return
     */
    public int hash16_plus(final String data) {  
        byte[] bytes = toBytesWithoutEncoding(data);  
        int hash = hash32(bytes, bytes.length, 0x9747b28c);  
        hash = hash % 65535 ;
        if(hash < 0){
            hash = - hash ;
        }
        return hash ;
    }  
    
    /** 
     * Generates 32 bit hash from byte array with default seed value.
     * @param  data byte array to hash  
     * @param length length of the array to hash  
     * @return 32 bit hash of the given array   
     */  
    public int hash32(final byte[] data, int length) {  
        return hash32(data, length, 0x9747b28c);  
    }  
      
    public int hash32(final String data) {  
        byte[] bytes = toBytesWithoutEncoding(data);  
        return hash32(bytes, bytes.length, 0x9747b28c);  
    }  
    
    /** 
     * Generates 64 bit hash from byte array with default seed value.  
     * @param  data byte array to hash 
     * @param length length of the array to hash  
     * @return  64 bit hash of the given string   
     */  
    public long hash64(final byte[] data, int length) {  
        return hash64(data, length, 0xe17a1465);  
    }  
      
      
    public long hash64(final String data) {  
        byte[] bytes = toBytesWithoutEncoding(data);  
        return hash64(bytes, bytes.length);  
    }  
    /** 
     * Generates 32 bit hash from byte array of the given length and seed. 
     * @param data byte array to hash  
     * @param length length of the array
     * @param seed initial seed value  
     * @return 32 bit hash of the given array   
     */  
    public int hash32(final byte[] data, int length, int seed) {  
        // 'm' and 'r' are mixing constants generated offline.  
        // They're not really 'magic', they just happen to work well.  
        final int m = 0x5bd1e995;  
        final int r = 24;  
        // Initialize the hash to a random value  
        int h = seed ^ length;  
        int length4 = length / 4;  
        for (int i = 0; i < length4; i++) {  
            final int i4 = i * 4;  
            int k = (data[i4 + 0] & 0xff) + ((data[i4 + 1] & 0xff) << 8)  
                    + ((data[i4 + 2] & 0xff) << 16)  
                    + ((data[i4 + 3] & 0xff) << 24);  
            k *= m;  
            k ^= k >>> r;  
            k *= m;  
            h *= m;  
            h ^= k;  
        }  
        // Handle the last few bytes of the input array  
        switch (length % 4) {  
        case 3:  
            h ^= (data[(length & ~3) + 2] & 0xff) << 16;  
        case 2:  
            h ^= (data[(length & ~3) + 1] & 0xff) << 8;  
        case 1:  
            h ^= (data[length & ~3] & 0xff);  
            h *= m;  
        }  
        h ^= h >>> 13;  
        h *= m;  
        h ^= h >>> 15;  
        return h;  
    }  
    /** 
     * Generates 64 bit hash from byte array of the given length and seed.   
     * @param data byte array to hash 
     * @param length length of the array to hash  
     * @param seed initial seed value 
     * @return 64 bit hash of the given array   
     */  
    public long hash64(final byte[] data, int length, int seed) {  
        final long m = 0xc6a4a7935bd1e995L;  
        final int r = 47;  
        long h = (seed & 0xffffffffl) ^ (length * m);  
        int length8 = length / 8;  
        for (int i = 0; i < length8; i++) {  
            final int i8 = i * 8;  
            long k = ((long) data[i8 + 0] & 0xff)  
                    + (((long) data[i8 + 1] & 0xff) << 8)  
                    + (((long) data[i8 + 2] & 0xff) << 16)  
                    + (((long) data[i8 + 3] & 0xff) << 24)  
                    + (((long) data[i8 + 4] & 0xff) << 32)  
                    + (((long) data[i8 + 5] & 0xff) << 40)  
                    + (((long) data[i8 + 6] & 0xff) << 48)  
                    + (((long) data[i8 + 7] & 0xff) << 56);  
            k *= m;  
            k ^= k >>> r;  
            k *= m;  
            h ^= k;  
            h *= m;  
        }  
        switch (length % 8) {  
        case 7:  
            h ^= (long) (data[(length & ~7) + 6] & 0xff) << 48;  
        case 6:  
            h ^= (long) (data[(length & ~7) + 5] & 0xff) << 40;  
        case 5:  
            h ^= (long) (data[(length & ~7) + 4] & 0xff) << 32;  
        case 4:  
            h ^= (long) (data[(length & ~7) + 3] & 0xff) << 24;  
        case 3:  
            h ^= (long) (data[(length & ~7) + 2] & 0xff) << 16;  
        case 2:  
            h ^= (long) (data[(length & ~7) + 1] & 0xff) << 8;  
        case 1:  
            h ^= (long) (data[length & ~7] & 0xff);  
            h *= m;  
        }  
        ;  
        h ^= h >>> r;  
        h *= m;  
        h ^= h >>> r;  
        return h;  
    }  
    public static void main(String[] args) {
        test1() ;
        test2() ;
    }
   
    public static void test1() {
        String regionNum = "110000" ;
        String temp = "" ;
        String rtuAddr = "" ;
        
        int total0 = 0 ;
        int total1 = 0 ;
        int total2 = 0 ;
        int total3 = 0 ;
        int total4 = 0 ;
        int total5 = 0 ;
        int totalx = 0 ;
        int totaly = 0 ;
        MurmurHash mhash = new MurmurHash() ;
        Long start = System.currentTimeMillis() ;
        for(int i = 0 ; i < 10000; i++){
            temp = "" + i ;
            while(temp.length() < 5){
                temp = "0" + temp ;
            }
            rtuAddr = regionNum + temp ;
            int hash = mhash.hash16_plus(rtuAddr) ;
            if(hash < 0){
                total0++ ;
            }
            if(hash >= 0 && hash < 1000){
                total1++ ;
            }
            if(hash >= 1000 && hash < 2000){
                total2++ ;
            }
            if(hash >= 2000 && hash < 3000){
                total3++ ;
            }
            if(hash >= 3000 && hash < 4000){
                total4++ ;
            }
            if(hash >= 4000 && hash < 5000){
                total5++ ;
            }
            if(hash >= 64535 && hash < 65535){
                totalx++ ;
            }
            if(hash > 65535){
                totaly++ ;
            }
            //System.out.println(rtuAddr + "-" + crc);
        }
        Long end = System.currentTimeMillis() ;
        System.out.println("用时" + ":" + (end - start));
        System.out.println("0以下" + ":" + total0);
        System.out.println("0-1000" + ":" + total1);
        System.out.println("1000-2000" + ":" + total2);
        System.out.println("2000-3000" + ":" + total3);
        System.out.println("3000-4000" + ":" + total4);
        System.out.println("5000-6000" + ":" + total5);
        System.out.println("64535-65535" + ":" + totalx);
        System.out.println("65535以上" + ":" + totaly);
        System.out.println("=================");        
    }
    public static void test2() {
        String regionNum = "110000" ;
        String temp = "" ;
        String rtuAddr = "" ;
        
        int total1 = 0 ;
        int total2 = 0 ;
        int total3 = 0 ;
        int total4 = 0 ;
        int total5 = 0 ;
        MurmurHash mhash = new MurmurHash() ;
        Long start = System.currentTimeMillis() ;
        for(int i = 0 ; i < 10000; i++){
            temp = "" ;
            temp = "" + i ;
            while(temp.length() < 5){
                temp = "0" + temp ;
            }
            rtuAddr = regionNum + temp ;
            int hash = mhash.hash32(rtuAddr) ;
            if(hash > 0 && hash < 10000000){
                total1++ ;
            }
            if(hash >= 10000000 && hash < 20000000){
                total2++ ;
            }
            if(hash >= 20000000 && hash < 30000000){
                total3++ ;
            }
            if(hash >= 30000000 && hash < 40000000){
                total4++ ;
            }
            if(hash >= 40000000 && hash < 50000000){
                total5++ ;
            }
            //System.out.println(rtuAddr + "-" + crc);
        }
        Long end = System.currentTimeMillis() ;
        System.out.println("用时" + ":" + (end - start));
        System.out.println("0-10000000" + ":" + total1);
        System.out.println("10000000-20000000" + ":" + total2);
        System.out.println("20000000-30000000" + ":" + total3);
        System.out.println("30000000-40000000" + ":" + total4);
        System.out.println("50000000-60000000" + ":" + total5);
        System.out.println("=================");
    }
}