java-IO操作性能对比
在软件系统中,IO速度比内存速度慢,IO读写在很多情况下会是系统的瓶颈。
在java标准IO操作中,InputStream和OutputStream提供基于流的IO操作,以字节为处理单位;Reader和Writer实现了Buffered缓存,以字符为处理单位。
从Java1.4开始,增加NIO(New IO),增加缓存Buffer和通道Channel,以块为处理单位,是双向通道(可读可写,类似RandomAccessFile),支持锁和内存映射文件访问接口,大大提升了IO速度。
以下例子简单测试常见IO操作的性能速度。
- /**
- * 测试不同io操作速度
- *
- * @author peter_wang
- * @create-time 2014-6-4 下午12:52:48
- */
- public class SpeedTest {
- private static final String INPUT_FILE_PATH = "io_speed.txt";
- private static final String OUTPUT_FILE_PATH = "io_speed_copy.txt";
- /**
- * @param args
- */
- public static void main(String[] args) {
- long ioStreamTime1 = ioStreamCopy();
- System.out.println("io stream copy:" + ioStreamTime1);
- long ioStreamTime2 = bufferedStreamCopy();
- System.out.println("buffered stream copy:" + ioStreamTime2);
- long ioStreamTime3 = nioStreamCopy();
- System.out.println("nio stream copy:" + ioStreamTime3);
- long ioStreamTime4 = nioMemoryStreamCopy();
- System.out.println("nio memory stream copy:" + ioStreamTime4);
- }
- /**
- * 普通文件流读写
- *
- * @return 操作的时间
- */
- private static long ioStreamCopy() {
- long costTime = -1;
- FileInputStream is = null;
- FileOutputStream os = null;
- try {
- long startTime = System.currentTimeMillis();
- is = new FileInputStream(INPUT_FILE_PATH);
- os = new FileOutputStream(OUTPUT_FILE_PATH);
- int read = is.read();
- while (read != -1) {
- os.write(read);
- read = is.read();
- }
- long endTime = System.currentTimeMillis();
- costTime = endTime - startTime;
- }
- catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- try {
- if (is != null) {
- is.close();
- }
- if (os != null) {
- os.close();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- return costTime;
- }
- /**
- * 加入缓存的文件流读写, Reader默认实现缓存,只能读取字符文件,无法准确读取字节文件如图片视频等
- *
- * @return 操作的时间
- */
- private static long bufferedStreamCopy() {
- long costTime = -1;
- FileReader reader = null;
- FileWriter writer = null;
- try {
- long startTime = System.currentTimeMillis();
- reader = new FileReader(INPUT_FILE_PATH);
- writer = new FileWriter(OUTPUT_FILE_PATH);
- int read = -1;
- while ((read = reader.read()) != -1) {
- writer.write(read);
- }
- writer.flush();
- long endTime = System.currentTimeMillis();
- costTime = endTime - startTime;
- }
- catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- try {
- if (reader != null) {
- reader.close();
- }
- if (writer != null) {
- writer.close();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- return costTime;
- }
- /**
- * nio操作数据流
- *
- * @return 操作的时间
- */
- private static long nioStreamCopy() {
- long costTime = -1;
- FileInputStream is = null;
- FileOutputStream os = null;
- FileChannel fi = null;
- FileChannel fo = null;
- try {
- long startTime = System.currentTimeMillis();
- is = new FileInputStream(INPUT_FILE_PATH);
- os = new FileOutputStream(OUTPUT_FILE_PATH);
- fi = is.getChannel();
- fo = os.getChannel();
- ByteBuffer buffer = ByteBuffer.allocate(1024);
- while (true) {
- buffer.clear();
- int read = fi.read(buffer);
- if (read == -1) {
- break;
- }
- buffer.flip();
- fo.write(buffer);
- }
- long endTime = System.currentTimeMillis();
- costTime = endTime - startTime;
- }
- catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- try {
- if (fi != null) {
- fi.close();
- }
- if (fo != null) {
- fo.close();
- }
- if (is != null) {
- is.close();
- }
- if (os != null) {
- os.close();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- return costTime;
- }
- /**
- * nio内存映射操作数据流
- *
- * @return 操作的时间
- */
- private static long nioMemoryStreamCopy() {
- long costTime = -1;
- FileInputStream is = null;
- //映射文件输出必须用RandomAccessFile
- RandomAccessFile os = null;
- FileChannel fi = null;
- FileChannel fo = null;
- try {
- long startTime = System.currentTimeMillis();
- is = new FileInputStream(INPUT_FILE_PATH);
- os = new RandomAccessFile(OUTPUT_FILE_PATH, "rw");
- fi = is.getChannel();
- fo = os.getChannel();
- IntBuffer iIb=fi.map(FileChannel.MapMode.READ_ONLY, 0, fi.size()).asIntBuffer();
- IntBuffer oIb = fo.map(FileChannel.MapMode.READ_WRITE, 0, fo.size()).asIntBuffer();
- while(iIb.hasRemaining()){
- int read = iIb.get();
- oIb.put(read);
- }
- long endTime = System.currentTimeMillis();
- costTime = endTime - startTime;
- }
- catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- try {
- if (fi != null) {
- fi.close();
- }
- if (fo != null) {
- fo.close();
- }
- if (is != null) {
- is.close();
- }
- if (os != null) {
- os.close();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- return costTime;
- }
- }
运行结果:
- io stream copy:384
- buffered stream copy:125
- nio stream copy:12
- nio memory stream copy:10
结论分析:
最普通的InputStream操作耗时较长,增加了缓存后速度增加了,用了nio和内存映射访问文件,速度最快。
java-IO操作性能对比的更多相关文章
- java IO性能对比----read文件
本次对比内容为:(jdk1.8) fileInputStream:最基本的文件读取(带自己声明的缓冲区) dataInputStream:字节读取,在<java编程思想>一书中描述为使用最 ...
- Java IO编程全解(六)——4种I/O的对比与选型
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7804185.html 前面讲到:Java IO编程全解(五)--AIO编程 为了防止由于对一些技术概念和术语 ...
- Java NIO 学习笔记(七)----NIO/IO 的对比和总结
目录: Java NIO 学习笔记(一)----概述,Channel/Buffer Java NIO 学习笔记(二)----聚集和分散,通道到通道 Java NIO 学习笔记(三)----Select ...
- java io读取性能对比
背景 从最早bio的只支持阻塞的bio(同步阻塞) 到默认阻塞支持非阻塞nio(同步非阻塞+同步阻塞)(此时加入mmap类) 再到aio(异步非阻塞) 虽然这些api改变了调用模式,但真正执行效率上是 ...
- Java中的NIO和IO的对比分析
总的来说,java中的IO和NIO主要有三点区别: IO NIO 面向流 面向缓冲 阻塞IO 非阻塞IO 无 选择器(Selectors) 1.面向流与面向缓冲 Java NIO和IO之间第一个最大的 ...
- Java IO流之【缓冲流和文件流复制文件对比】
与文件流相比,缓冲流复制文件更快 代码: package Homework; import java.io.BufferedOutputStream; import java.io.File; imp ...
- JAVA IO 序列化与设计模式
➠更多技术干货请戳:听云博客 序列化 什么是序列化 序列化:保存对象的状态 反序列化:读取保存对象的状态 序列化和序列化是Java提供的一种保存恢复对象状态的机制 序列化有什么用 将数据保存到文件或数 ...
- Java IO面试
1. 讲讲IO里面的常见类,字节流.字符流.接口.实现类.方法阻塞. 字节流和字符流的区别: 1)字节流处理单元为1个字节,操作字节和字节数组,而字符流处理的单元为2个字节的Unicode字符,分别操 ...
- 关于SpringMVC项目报错:java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/xxxx.xml]
关于SpringMVC项目报错:java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/xxxx ...
- Java IO编程全解(五)——AIO编程
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7794151.html 前面讲到:Java IO编程全解(四)--NIO编程 NIO2.0引入了新的异步通道的 ...
随机推荐
- oracle的日期蛋
一切都是扯鸡巴蛋. 在网上查oracle的日期函数用法,得到一大堆语法,林林总总,都是扯鸡巴蛋,没能解决我的问题. 其实,我想写这么一条语句:查找某个日期(不含时分秒)产生或有关的记录.咋写? SQL ...
- 普通用户无法登陆SSH问题
Linux正常情况下普通用户是可以登陆SSH的,除非系统管理员作了修改,如果没有修改的情况无法登陆可以尝试以下方法解决: 步骤/方法 1 查看 /etc/ssh/sshd_config文件 发现 ro ...
- CentOS7.2 设置GRUB2引导界面分辨率
最近在学习OS引导启动,GRUB2的学习材料也不少,主要还看官方手册清晰些. 公司里办公机的多启动用的ubuntu的界面,还挺炫酷的.之前看其他博客网文里看到可以设置grub2的分辨率,我拿CentO ...
- 运算符:三目运算符,运算符优先级,sizeof,自增自减,取余
一://---------运算符-----------// 1.运算符是告诉编译程序执行特定算术或逻辑操作的符号. 2.按照功能划分: 算术运算符. 关系运算符与逻辑运算符.按位运算符. 3.运算符根 ...
- Codeforces 633H. Fibonacci-ish II
题目大意: 一个数列 q次询问 每次询问l r 将数列中l-r的位置排序去重后的数列成为b 输出 sigma b i * F i (其中F i为斐波那契数列中的第i项) 思路: 由于要去重 考虑权值线 ...
- sqlserver2008Mail
use msdb GO DROP PROC MailTim GO CREATE PROC MailTim @Subject NVARCHAR(100),@Body NVARCHAR( ...
- UI:使用 pod 引入 AFNetworking
cocdpods的安装 参考1 参考2 参考3 注意:MVC是一种搭建项目的思想,不是设计模式. 使用第三方管理控件: 引入CocoaPods的详细步骤:(1)检测有没有引入淘宝镜像gem sou ...
- 返回一个集合对象,同时这个集合的对象的属性又是一个集合对象的处理方法(ViewModel)
如果遇到需要返回一个集合对象,其中该集合中的属性又是一个集合.第一种:可在考虑用外键关联,比如在控制器中可以采用预先加载的方式加载关联的数据,比如 RoleManager.Roles.Include& ...
- Mysql 告警 :Establishing SSL connection without server's identity verification is not recommended.
在集成spring与mybatis是,在spring.xml中配置了DataSource配置,数据库连接采用的是mysql的链接字符串: jdbc:mysql://localhost:3306/wor ...
- VMware 12安装Mac OS X 10.11&解决上网的问题
近日想在Win10上安装Mac OS 玩玩,于是上网搜了相关资源,查看了相关经验分享,开始着手安装.系统很快成功安装,但最大问题是虚拟机中的Mac OS无法上网.费了很长时间,最终看到Ping通结果, ...