增加MetroHash算法

This commit is contained in:
lsx0310
2022-01-24 23:53:26 +08:00
parent b6c73dd010
commit 74c1aa12a3
5 changed files with 428 additions and 1 deletions

View File

@@ -0,0 +1,81 @@
package cn.hutool.core.lang;
import cn.hutool.core.lang.hash.CityHash;
import cn.hutool.core.lang.hash.MetroHash;
import org.junit.Test;
import java.util.Random;
public class MetroHashTest {
/**
* 数据量越大 MetroHash 优势越明显,
*/
@Test
public void bulkHashing64Test() {
String[] strArray = getRandomStringArray(10000000);
long startCity = System.currentTimeMillis();
for (String s : strArray) {
CityHash.hash64(s.getBytes());
}
long endCity = System.currentTimeMillis();
long startMetro = System.currentTimeMillis();
for (String s : strArray) {
MetroHash.hash64(s);
}
long endMetro = System.currentTimeMillis();
System.out.println("metroHash =============" + (endMetro - startMetro));
System.out.println("cityHash =============" + (endCity - startCity));
}
/**
* 数据量越大 MetroHash 优势越明显,
*/
@Test
public void bulkHashing128Test() {
String[] strArray = getRandomStringArray(10000000);
long startCity = System.currentTimeMillis();
for (String s : strArray) {
CityHash.hash128(s.getBytes());
}
long endCity = System.currentTimeMillis();
long startMetro = System.currentTimeMillis();
for (String s : strArray) {
MetroHash.hash128(s);
}
long endMetro = System.currentTimeMillis();
System.out.println("metroHash =============" + (endMetro - startMetro));
System.out.println("cityHash =============" + (endCity - startCity));
}
private static String[] getRandomStringArray(int length) {
String[] result = new String[length];
Random random = new Random();
int index = 0;
while (index < length) {
result[index++] = getRandomString(random.nextInt(64));
}
return result;
}
private static String getRandomString(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
}
}