首页 / JAVA / Java NIO与非NIO性能
Java NIO与非NIO性能
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java NIO与非NIO性能,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4151字,纯文字阅读大概需要6分钟。
内容图文
我花了相当多的时间来尝试优化文件哈希算法,以便尽可能地了解最后一滴性能.
查看我以前的SO主题:
Get File Hash Performance/Optimization
FileChannel ByteBuffer and Hashing Files
Determining Appropriate Buffer Size
建议多次使用Java NIO来获得本机性能提升(通过将缓冲区保留在系统中而不是将它们引入JVM).但是,我的NIO代码在基准测试中运行得相当慢(使用每种算法反复散列相同的文件,以消除任何可能导致结果偏差的操作系统/驱动器“魔力”.
我现在有两种方法可以做同样的事情:
这个几乎每次都跑得更快:
/**
* 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);
}
}
我的Java NIO方法在大多数情况下运行速度相当慢:
/**
* 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引入缓冲区,否定了本机缓冲区/系统调用带来的任何性能改进?我在这个逻辑中是正确的还是我离开了?
请帮助我理解为什么Java NIO在这种情况下不是更好.
解决方法:
有两件事可能会让你的NIO方法变得更好:
>尝试使用memory-mapped file而不是将数据读入堆内存.
>将数据传递给摘要using a ByteBuffer而不是byte []数组.
第一个应该避免在文件缓存和应用程序堆之间复制数据,而第二个应该避免在缓冲区和字节数组之间复制数据.如果没有这些优化,您可能会有更多复制,这是一种天真的非NIO方法.
内容总结
以上是互联网集市为您收集整理的Java NIO与非NIO性能全部内容,希望文章能够帮你解决Java NIO与非NIO性能所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。