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引入了新的异步通道的 ...
随机推荐
- V4L学习
http://blog.csdn.net/wangrunmin/article/details/7764768# http://blog.sina.com.cn/s/blog_a44175a90101 ...
- java元组-泛型
需要组合对象的时候使用元组可以简化代码,不需要每当需要组合类的时候都去创建一个新的对象.单元素就是常见的泛型,可以两个三个到多个元素:元组可以继承:java泛型不能使用基本类型如int long 必须 ...
- “Invalid configuration file. File "I:/My Virtual Machines/Windows XP english Professional/Windows XP Professional.vmx" was created by a VMware product
“Invalid configuration file. File "I:/My Virtual Machines/Windows XP english Professional/Windo ...
- linux怎么区别文本文件和二进制文件
linux的文本文件与二进制文件的区分与windows的区分是相同的!说到底计算机存储的文件都是以二进制形式存储的,但是区别是,习惯上认为: (1).文本文件 文本文件是包含用户可读信息的文件.这些文 ...
- YTU 2562: 黄金螺旋
2562: 黄金螺旋 时间限制: 1 Sec 内存限制: 128 MB 提交: 832 解决: 427 题目描述 黄金螺旋是根据斐波那契数列画出来的螺旋曲线,自然界中存在许多斐波那契螺旋线的图案, ...
- AngularJS 指令实践指南(二)
这个系列教程的第一部分给出了AngularJS指令的基本概述,在文章的最后我们介绍了如何隔离一个指令的scope.第二部分将承接上一篇继续介绍.首先,我们会看到在使用隔离scope的情况下,如何从指令 ...
- jqplot配置参考
jqPlot整的来说有三个地方需要配置.格式如: $.jqplot(‘target’, data, options);target:要显示的位置.data:显示的数据.options:其它配置 ...
- [USACO 2017DEC] Greedy Gift Takers
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5139 [算法] 二分答案 时间复杂度 : O(NlogN^2) [代码] #incl ...
- asp.net mvc 多字段排序
以下代码可实现多字段排序,通过点击列标题,实现排序. 控制器: public ActionResult Index(string sortOrder) { ViewBag.FirstNameSortP ...
- c语言struct和c++的class的暧昧
c语言风格的封装 数据放在一起,以引用和指针的方式传给行为c++ 认为封装不彻底 1数据和行为分开 对外提供接口 2没有权限设置 看看struct的一个例子 //data.h //c语言风格的封装 数 ...