Java--Stream,NIO ByteBuffer,NIO MappedByteBuffer性能对比
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java--Stream,NIO ByteBuffer,NIO MappedByteBuffer性能对比,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3638字,纯文字阅读大概需要6分钟。
内容图文
![Java--Stream,NIO ByteBuffer,NIO MappedByteBuffer性能对比](/upload/InfoBanner/zyjiaocheng/1298/d6c22e72600a428db25e34ea406fdb5b.jpg)
目前Java中最IO有多种文件读取的方法,本文章对比Stream,NIO ByteBuffer,NIO MappedByteBuffer的性能,让我们知道到底怎么能写出性能高的文件读取代码。
package com.seeyon.nio; import org.junit.Test; import java.io.*; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; /** * Created by yangyu on 16/12/28. *//** * 比较Stream流,NIO ByteBuffer,NIO MappedByteBuffer性能对比 * 其中Stream最慢,NIO MappedByteBuffer最快 * Stream:1000ms * NIO ByteBuffer:220ms * NIO MappedByteBuffer:112ms */publicclass Compare { /** * 使用stream作为IO流读取和写入文件 * 速度:1000ms左右 * * @throws IOException */ @Test publicvoid useStream() throws IOException { long startTime = System.currentTimeMillis(); /** * 4000000个整数长度的文件 */int num = 2000 * 2000; /** * 带缓冲的输出流,写文件 */ DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("/Users/yangyu/Downloads/compare.tmp"))); for (int i = 0; i < num; i++) { dataOutputStream.writeInt(i); } dataOutputStream.close(); int data = 0; /** * 带缓冲的输入流,读文件 */ DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("/Users/yangyu/Downloads/compare.tmp"))); try { while (true) { data = in.readInt(); } } catch (EOFException e) { System.out.println("读取完成"+data); } in.close(); long endTime = System.currentTimeMillis(); System.out.println("ms:" + (endTime - startTime)); } /** * 使用NIO ByteBuffer * 时间:220ms * @throws IOException */ @Test publicvoid useNioByteBuffer() throws IOException { long startTime = System.currentTimeMillis(); int num = 2000*2000; /** * 文件输出流 */ FileOutputStream fileOutputStream = new FileOutputStream("/Users/yangyu/Downloads/compare.tmp"); /** * NIO Channel 通道 */ FileChannel fileChannel = fileOutputStream.getChannel(); /** * ByteBuffer缓冲区 */ ByteBuffer buffer = ByteBuffer.allocate(num*5); for (int i = 0; i < num; i++) { buffer.putInt(i); } /** * 为写做准备 */ buffer.flip(); /** * 写操作 */ fileChannel.write(buffer); fileChannel.close(); /** * 缓冲区 */ ByteBuffer buffer1 = ByteBuffer.allocate(num*5); /** * 文件输入流 */ FileInputStream in = new FileInputStream("/Users/yangyu/Downloads/compare.tmp"); /** * 输入通道 */ FileChannel fin = in.getChannel(); /** * 为读取做准备 */ buffer1.clear(); System.out.println(buffer1.limit()); /** * 读取 */ fin.read(buffer1); fin.close(); long endTime = System.currentTimeMillis(); System.out.println("ms:" + (endTime - startTime)); buffer1.flip(); System.out.println(buffer1.limit()); } /** * 使用MappedByteBuffer,通过FileChannel将文件映射到内存 * 时间:112ms * @throws IOException */ @Test publicvoid useRandomAccess() throws IOException { long startTime = System.currentTimeMillis(); int num = 2000*2000; /** * 使用可随机访问位置的RandomAccessFile */ RandomAccessFile file = new RandomAccessFile("/Users/yangyu/Downloads/compare.tmp","rw"); /** * 获取通道Channel */ FileChannel fileChannel = file.getChannel(); /** * 将文件映射到缓冲区MappedByteBuffer */ MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE,0,num*4); /** * 写文件 */for (int i = 0; i < num; i++) { mappedByteBuffer.putInt(i); } fileChannel.close(); int data=0; RandomAccessFile file1 = new RandomAccessFile("/Users/yangyu/Downloads/compare.tmp","rw"); FileChannel fc = file1.getChannel(); MappedByteBuffer mappedByteBuffer1 = fc.map(FileChannel.MapMode.READ_WRITE,0,file1.length()); /** * 读文件 */while (mappedByteBuffer1.hasRemaining()){ data = mappedByteBuffer1.getInt(); } fc.close(); long endTime = System.currentTimeMillis(); System.out.println("ms:" + (endTime - startTime)); System.out.println(data); } }
结论非常明显啦,以后再使用IO读写文件的时候,多使用NIO MappedByteBuffer吧,毕竟NIO比老IO性能好太多啦。
原文:http://www.cnblogs.com/eoss/p/6229587.html
内容总结
以上是互联网集市为您收集整理的Java--Stream,NIO ByteBuffer,NIO MappedByteBuffer性能对比全部内容,希望文章能够帮你解决Java--Stream,NIO ByteBuffer,NIO MappedByteBuffer性能对比所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。