Java--Stream,NIO ByteBuffer,NIO MappedByteBuffer性能对比
目前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
*/
public class Compare { /**
* 使用stream作为IO流读取和写入文件
* 速度:1000ms左右
*
* @throws IOException
*/
@Test
public void 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
public void 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
public void 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性能好太多啦。
Java--Stream,NIO ByteBuffer,NIO MappedByteBuffer性能对比的更多相关文章
- NIO与普通IO文件读写性能对比
最近在熟悉java的nio功能.nio采用了缓冲区的方式进行文件的读写,这一点更接近于OS执行I/O的方式.写了个新旧I/O复制文件的代码,练练手,顺便验证一下两者读写性能的对比,nio是否真的比普通 ...
- java.nio.ByteBuffer中的flip()、rewind()、compact()等方法的使用和区别
java.nio.ByteBuffer 1. ByteBuffer中的参数position.limit.capacity.mark含义: position:表示当前指针的位置(下一个要操作的数据元素的 ...
- 5种调优Java NIO和NIO.2的方式
Java NIO(New Input/Output)——新的输入/输出API包——是2002年引入到J2SE 1.4里的.Java NIO的目标是提高Java平台上的I/O密集型任务的性能.过了十年, ...
- Java的BIO,NIO,AIO
Java中的IO操作可谓常见.在Java的IO体系中,常有些名词容易让人困惑不解.为此,先通俗地介绍下这些名词. 1 什么是同步? 2 什么是异步? 3 什么是阻塞? 4 什么是非阻塞? 5 什么是同 ...
- 顺序、随机IO和Java多种读写文件性能对比
概述 对于磁盘的读写分为两种模式,顺序IO和随机IO. 随机IO存在一个寻址的过程,所以效率比较低.而顺序IO,相当于有一个物理索引,在读取的时候不需要寻找地址,效率很高. 基本流程 总体结构 我们编 ...
- Atitit.病毒木马的快速扩散机制原理nio 内存映射MappedByteBuffer
Atitit.病毒木马的快速扩散机制原理nio 内存映射MappedByteBuffer 1. Java NIO(New Input/Output)1 1.1. 变更通知(因为每个事件都需要一个监听者 ...
- Java NIO、NIO.2学习笔记
相关学习资料 http://www.molotang.com/articles/903.html http://www.ibm.com/developerworks/cn/education/java ...
- 【转载】Java NIO学习 & NIO BIO AIO 比较
可以参考这个页面: http://www.iteye.com/magazines/132-Java-NIO (下面这个页面也有) http://ifeve.com/overview/ 另,在这篇文章里 ...
- java学习-NIO(五)NIO学习总结以及NIO新特性介绍
我们知道是NIO是在2002年引入到J2SE 1.4里的,很多Java开发者比如我还是不知道怎么充分利用NIO,更少的人知道在Java SE 7里引入了更新的输入/输出 API(NIO.2).但是对于 ...
随机推荐
- 如何使用Worktile进行敏捷项目开发管理
Worktile在任务管理上采用了看板视图,非常适合进行敏捷项目开发管理.事实上,在开发Worktile的过程中,我们也是自产自销,使用Worktile管理Worktile本身的开发过程,在本文中跟大 ...
- Linux守护进程之Supervisor
1. 什么是守护进程 在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件.由于在linux中,每个 ...
- PHP钩子机制
什么是钩子 大家想必听过插件,wordpress插件特别多,这个就是用钩子机制实现的. 当代码在运行的过程中,我们预先在运行的几个特殊点里执行一些特殊方法:例如在运行方法(例如Blog::add的ad ...
- java IO流 之 字符流
字符是我们能读懂的一些文字和符号,但在计算机中存储的却是我们看不懂的byte 字节,那这就存在关于字符编码解码的问题.所以在学习Io流的字符流前我们先了解些关于编码问题. 一.字符集与字符编码 1.什 ...
- 改变textView的个别字体颜色
Spannable span = new SpannableString(getString(R.string.register_need_to_ageree));//例如:register_need ...
- KnockoutJS 3.X API 第七章 其他技术(7) 微任务
注意:本文档适用于Knockout 3.4.0及更高版本. Knockout的微任务队列 Knockout的微任务队列支持调度任务尽可能快地运行,同时仍然是异步的,努力安排它们在发生I / O,回流或 ...
- Css概要与选择器,刻度单位
目录 一.CSS3概要 1.1.特点 1.2.效果演示 1.3.帮助文档与学习 二.选择器 1.1.基础的选择器 1.2.组合选择器 1.3.属性选择器 1.4.伪类 1.5.伪元素 三.特殊性(优先 ...
- Index的填充属性:FillFactor 和 PAD_INDEX
在Create Index时,必须考虑属性FillFactor 和 PAD_INDEX的设置,这两个属性只在create index 或 rebuild index时起作用,表示Index page( ...
- Clash Detection
Clash Detection eryar@163.com Abstract. Clash detection is used for the model collision check. The p ...
- Service Plugin / Agent - 每天5分钟玩转 OpenStack(73)
Core Plugin/Agent 负责管理核心实体:net, subnet 和 port.而对于更高级的网络服务,则由 Service Plugin/Agent 管理.Service Plugin ...