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引入了新的异步通道的 ...
随机推荐
- C ++模板的声明和实现为何要放在头文件中?
源: http://blog.csdn.net/lqk1985/archive/2008/10/24/3136364.aspx 如何组织编写模板程序 发表日期: 1/21/2003 12:28:58 ...
- CAS和ABA
1 CAS compare and swap的缩写,详见乐观锁和悲观锁. 2 ABA 就是说,我获取的旧值是A,然后被人修改成了B,但是又被人修改成了A,我就认为并没有修改,更新内存. 解决办法,给每 ...
- linux php nginx php-fpm 关系 动态进程生成
yum install php yum install php-fpm 启动fpm [root@VM_141_64_centos html]# service php-fpm restart Redi ...
- Semantic Parsing(语义分析) Knowledge base(知识图谱) 对用户的问题进行语义理解 信息检索方法
简单说一下所谓Knowledge base(知识图谱)有两条路走,一条是对用户的问题进行语义理解,一般用Semantic Parsing(语义分析),语义分析有很多种,比如有用CCG.DCS,也有用机 ...
- HDU3038 How Many Answers Are Wrong —— 带权并查集
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...
- usdt源码编译安装
1.依赖关系Boost >= 1.53 2.安装依赖包You will need appropriate libraries to run Omni Core on Unix, please s ...
- 输出两个MAC地址之间的地址
/******************************************************************************* * 输出两个MAC地址之间的地址 * ...
- BZOJ3160【万径人踪灭】 【FFT】
..恩 打了四五遍 不会也背出来了.. BZOJ3160 [听说时限紧?转C++的优势么?] 上AC代码 fft /*Problem: 3160 User: cyz666 Language: C++ ...
- Go语言web框架 gin
Go语言web框架 GIN gin是go语言环境下的一个web框架, 它类似于Martini, 官方声称它比Martini有更好的性能, 比Martini快40倍, Ohhhh….看着不错的样子, 所 ...
- bzoj3668 [Noi2014]起床困难综合症——贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3668 一开始想着倒序推回去看看这一位能不能达到来着,因为这样好中途退出(以为不这样会T): ...