【发布时间】:2013-05-02 02:41:24
【问题描述】:
我花费了大量时间尝试优化文件散列算法,以尽可能降低性能。
查看我之前的 SO 主题:
Get File Hash Performance/Optimization
FileChannel ByteBuffer and Hashing Files
Determining Appropriate Buffer Size
多次推荐使用Java NIO 来获得本机性能提升(通过将缓冲区保留在系统中而不是将它们带入JVM)。但是,我的 NIO 代码在非基准测试中运行速度要慢得多(使用每种算法一遍又一遍地散列相同的文件,以否定任何可能导致结果偏差的操作系统/驱动器“魔法”。
我现在有两种方法可以做同样的事情:
This one runs faster almost every time:
/**
* Gets Hash of file.
*
* @param file String path + filename of file to get hash.
* @param hashAlgo Hash algorithm to use. <br/>
* Supported algorithms are: <br/>
* MD2, MD5 <br/>
* SHA-1 <br/>
* SHA-256, SHA-384, SHA-512
* @param BUFFER Buffer size in bytes. Recommended to stay in<br/>
* multiples of 2 such as 1024, 2048, <br/>
* 4096, 8192, 16384, 32768, 65536, etc.
* @return String value of hash. (Variable length dependent on hash algorithm used)
* @throws IOException If file is invalid.
* @throws HashTypeException If no supported or valid hash algorithm was found.
*/
public String getHash(String file, String hashAlgo, int BUFFER) throws IOException, HasherException {
StringBuffer hexString = null;
try {
MessageDigest md = MessageDigest.getInstance(validateHashType(hashAlgo));
FileInputStream fis = new FileInputStream(file);
byte[] dataBytes = new byte[BUFFER];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
fis.close();
byte[] mdbytes = md.digest();
hexString = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++) {
hexString.append(Integer.toHexString((0xFF & mdbytes[i])));
}
return hexString.toString();
} catch (NoSuchAlgorithmException | HasherException e) {
throw new HasherException("Unsuppored Hash Algorithm.", e);
}
}
My Java NIO method that runs considerably slower most of the time:
/**
* Gets Hash of file using java.nio File Channels and ByteBuffer
* <br/>for native system calls where possible. This may improve <br/>
* performance in some circumstances.
*
* @param fileStr String path + filename of file to get hash.
* @param hashAlgo Hash algorithm to use. <br/>
* Supported algorithms are: <br/>
* MD2, MD5 <br/>
* SHA-1 <br/>
* SHA-256, SHA-384, SHA-512
* @param BUFFER Buffer size in bytes. Recommended to stay in<br/>
* multiples of 2 such as 1024, 2048, <br/>
* 4096, 8192, 16384, 32768, 65536, etc.
* @return String value of hash. (Variable length dependent on hash algorithm used)
* @throws IOException If file is invalid.
* @throws HashTypeException If no supported or valid hash algorithm was found.
*/
public String getHashNIO(String fileStr, String hashAlgo, int BUFFER) throws IOException, HasherException {
File file = new File(fileStr);
MessageDigest md = null;
FileInputStream fis = null;
FileChannel fc = null;
ByteBuffer bbf = null;
StringBuilder hexString = null;
try {
md = MessageDigest.getInstance(hashAlgo);
fis = new FileInputStream(file);
fc = fis.getChannel();
bbf = ByteBuffer.allocateDirect(BUFFER); // allocation in bytes - 1024, 2048, 4096, 8192
int b;
b = fc.read(bbf);
while ((b != -1) && (b != 0)) {
bbf.flip();
byte[] bytes = new byte[b];
bbf.get(bytes);
md.update(bytes, 0, b);
bbf.clear();
b = fc.read(bbf);
}
fis.close();
byte[] mdbytes = md.digest();
hexString = new StringBuilder();
for (int i = 0; i < mdbytes.length; i++) {
hexString.append(Integer.toHexString((0xFF & mdbytes[i])));
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new HasherException("Unsupported Hash Algorithm.", e);
}
}
我的想法是Java NIO 尝试使用本机系统调用等来保持系统中的处理和存储(缓冲区)以及 JVM 之外 - 这可以防止(理论上)程序不得不不断地重新洗牌在 JVM 和系统之间来回切换。理论上这应该更快......但也许我的MessageDigest 强制 JVM 引入缓冲区,否定本机缓冲区/系统调用可以带来的任何性能改进?我在这个逻辑上是正确的还是我离题了?
Please help me understand why Java NIO is not better in this scenario.
【问题讨论】:
-
NIO 擅长并发。如果您没有并发执行,它只会给您带来代码和处理方面的开销。我不知道性能差异是什么,但可能就是这样。
-
从通道读取到
ByteBuffer然后再次复制到byte[]可能会损害nio 方法的性能。如果 JVM 可以避免将数据从系统空间(操作系统、磁盘等)传输到用户空间,NIO 魔法(除了非阻塞部分)主要发挥作用。由于散列算法需要读取文件的每个字节,显然没有可用的捷径。如果 Old-IO 方法的性能不够,请考虑使用分析器或测试库实现(例如,guava 以获得更好的性能 -
@Pyranja 嗯,有趣的信息。我检查了 Guava 库,不幸的是,我没有预见到它会比我上面的方法产生任何性能提升,因为它们都依赖于默认的
java.security.MessageDigest实现来实际执行哈希......如果文件太大而无法修复合理地在缓冲区中(例如 10GB 文件),那么它必须通过缓冲区进行流式传输,并且我们必须执行许多 I/O 操作才能通过缓冲区进行流式传输,直到我们对所有位进行哈希处理。 . 嗯.. -
@akostadinov 这是否适用于所有蔚来汽车?除了非阻塞通道之外,它还有更多功能。