所以我一直在寻找一种更快的方法来计算 MD5 校验和并运行Fast MD5 库——但是当我在我的机器上使用 Java 7 对其进行基准测试时,它的运行速度比 Java 版本慢。
要么我在做一些愚蠢的事情(很可能),要么 Java 7 实现了更好的算法(也很可能)。这是我超级简单的“基准”——也许我今天没有喝足够的咖啡……
MD5 digest = new MD5();
System.out.println(MD5.initNativeLibrary(true));
byte[] buff = IOUtils.readFully(new FileInputStream(new File("blahblah.bin")), 64000000, true);
ByteBuffer buffer = ByteBuffer.wrap(buff);
for (int j = 0; j < 100; j++) {
start = System.currentTimeMillis();
String md5Base64 = Utilities.getDigestBase64(buffer);
end = System.currentTimeMillis();
total = total + (end-start);
}
System.out.println("Took " + ((total)/100.00) + " ms. for " + buff.length+" bytes");
total = 0;
for (int i = 0; i < 100; i++) {
start = System.currentTimeMillis();
digest.Init();
digest.Update(buff);
digest.Final();
end = System.currentTimeMillis();
total = total + (end-start);
}
System.out.println("Took " + ((total)/100.00) + " ms. for " + buff.length+" bytes");
我得到:
Took 247.99 ms. for 64000000 bytes
Took 295.16 ms. for 64000000 bytes
根据评论,我一遍又一遍地运行基准测试并得到最奇怪的结果。FastMD5 计算保持不变,但 Java 7 版本变慢了。???
Took 246.54 ms. for 64000000 bytes
Took 294.69 ms. for 64000000 bytes
************************************
Took 540.55 ms. for 64000000 bytes
Took 292.69 ms. for 64000000 bytes
************************************
Took 537.07 ms. for 64000000 bytes
Took 292.12 ms. for 64000000 bytes