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引入了新的异步通道的 ...
随机推荐
- VS创建Web项目提示配置IISExpress失败
开发服务器VS2013,新建Web项目提示: 打开Web项目提示: 解决方法:控制面板,找到IISExpress,右键 选择修复,解决问题..
- HDU 5056 Boring count(不超过k个字符的子串个数)
Boring count Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- XMU C语言程序设计实践(2)
任务一:颠倒的世界 小明最近突然喜欢倒着写字,写出来的句子全是颠倒的,也就是把一句话里的字符全都逆序写,譬如“I Love This Game!”,他就偏偏要写成“!emaG sihT evoL I” ...
- Ubuntu下Jupyter Notebook的安装
pip install --upgrade pip //更新pip pip install jupyter sudo apt install jupyter-notebook 运行 jupyter-n ...
- Expression Blend实例中文教程系列
Expression Blend实例中文教程系列 本系列文章均转载:银光中国 时间:2010-04-09 09:20责任编辑:银光中国网 点击:次 Expression Blend实例中文教程系列由C ...
- python操作dataFrame
python数据分析工具pandas中DataFrame和Series作为主要的数据结构. 本文主要是介绍如何对DataFrame数据进行操作并结合一个实例测试操作函数. 1)查看DataFrame数 ...
- 四:多线程--NSOperation简单介绍
一.NSOperation简介 1.NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQu ...
- 05_传智播客iOS视频教程_第一个OC程序
Cocoa Application开发的是带界面的程序. OC是完全兼容C语言的,但是C语言里面是不能写OC的东西的. OC和C的第一个区别,就是源文件的后缀名的区别.OC程序的源文件的后缀名是.m, ...
- Linux 系统管理命令 - iftop - 动态显示网络接口流量信息
命令详解 重要星级: ★★★★☆ 功能说明: iftop 是一款实时流量监控工具,可用于监控 TCP/IP 连接等,必须以 root 用户的身份运行.一般最小化安装系统是没有这个命令的,需要使用 yu ...
- MySQL之不得不说的keepsync和trysync
此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 开宗明义,keepsync和trysync是网易MySQL分支版本InnoSQL的两个参数,非常重要的两个参 ...