java io顺序_Java顺序IO性能
java io顺序
- 使用页大小的普通字节[]的RandomAccessFile 。
- 缓冲的FileInputStream和FileOutputStream 。
- 具有页面大小的ByteBuffer的NIO FileChannel 。
- 内存使用NIO和直接MappedByteBuffer映射文件。
代码
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;import static java.lang.Integer.MAX_VALUE;
import static java.lang.System.out;
import static java.nio.channels.FileChannel.MapMode.READ_ONLY;
import static java.nio.channels.FileChannel.MapMode.READ_WRITE;public final class TestSequentialIoPerf
{public static final int PAGE_SIZE = 1024 * 4;public static final long FILE_SIZE = PAGE_SIZE * 2000L * 1000L;public static final String FILE_NAME = "test.dat";public static final byte[] BLANK_PAGE = new byte[PAGE_SIZE];public static void main(final String[] arg) throws Exception{preallocateTestFile(FILE_NAME);for (final PerfTestCase testCase : testCases){for (int i = 0; i < 5; i++){System.gc();long writeDurationMs = testCase.test(PerfTestCase.Type.WRITE,FILE_NAME);System.gc();long readDurationMs = testCase.test(PerfTestCase.Type.READ,FILE_NAME);long bytesReadPerSec = (FILE_SIZE * 1000L) / readDurationMs;long bytesWrittenPerSec = (FILE_SIZE * 1000L) / writeDurationMs;out.format("%s\twrite=%,d\tread=%,d bytes/sec\n",testCase.getName(),bytesWrittenPerSec, bytesReadPerSec);}}deleteFile(FILE_NAME);}private static void preallocateTestFile(final String fileName)throws Exception{RandomAccessFile file = new RandomAccessFile(fileName, "rw");for (long i = 0; i < FILE_SIZE; i += PAGE_SIZE){file.write(BLANK_PAGE, 0, PAGE_SIZE);}file.close();}private static void deleteFile(final String testFileName) throws Exception{File file = new File(testFileName);if (!file.delete()){out.println("Failed to delete test file=" + testFileName);out.println("Windows does not allow mapped files to be deleted.");}}public abstract static class PerfTestCase{public enum Type { READ, WRITE }private final String name;private int checkSum;public PerfTestCase(final String name){this.name = name;}public String getName(){return name;}public long test(final Type type, final String fileName){long start = System.currentTimeMillis();try{switch (type){case WRITE:{checkSum = testWrite(fileName);break;}case READ:{final int checkSum = testRead(fileName);if (checkSum != this.checkSum){final String msg = getName() +" expected=" + this.checkSum +" got=" + checkSum;throw new IllegalStateException(msg);}break;}}}catch (Exception ex){ex.printStackTrace();}return System.currentTimeMillis() - start;}public abstract int testWrite(final String fileName) throws Exception;public abstract int testRead(final String fileName) throws Exception;}private static PerfTestCase[] testCases ={new PerfTestCase("RandomAccessFile"){public int testWrite(final String fileName) throws Exception{RandomAccessFile file = new RandomAccessFile(fileName, "rw");final byte[] buffer = new byte[PAGE_SIZE];int pos = 0;int checkSum = 0;for (long i = 0; i < FILE_SIZE; i++){byte b = (byte)i;checkSum += b;buffer[pos++] = b;if (PAGE_SIZE == pos){file.write(buffer, 0, PAGE_SIZE);pos = 0;}}file.close();return checkSum;}public int testRead(final String fileName) throws Exception{RandomAccessFile file = new RandomAccessFile(fileName, "r");final byte[] buffer = new byte[PAGE_SIZE];int checkSum = 0;int bytesRead;while (-1 != (bytesRead = file.read(buffer))){for (int i = 0; i < bytesRead; i++){checkSum += buffer[i];}}file.close();return checkSum;}},new PerfTestCase("BufferedStreamFile"){public int testWrite(final String fileName) throws Exception{int checkSum = 0;OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));for (long i = 0; i < FILE_SIZE; i++){byte b = (byte)i;checkSum += b;out.write(b);}out.close();return checkSum;}public int testRead(final String fileName) throws Exception{int checkSum = 0;InputStream in = new BufferedInputStream(new FileInputStream(fileName));int b;while (-1 != (b = in.read())){checkSum += (byte)b;}in.close();return checkSum;}},new PerfTestCase("BufferedChannelFile"){public int testWrite(final String fileName) throws Exception{FileChannel channel = new RandomAccessFile(fileName, "rw").getChannel();ByteBuffer buffer = ByteBuffer.allocate(PAGE_SIZE);int checkSum = 0;for (long i = 0; i < FILE_SIZE; i++){byte b = (byte)i;checkSum += b;buffer.put(b);if (!buffer.hasRemaining()){buffer.flip();channel.write(buffer);buffer.clear();}}channel.close();return checkSum;}public int testRead(final String fileName) throws Exception{FileChannel channel = new RandomAccessFile(fileName, "rw").getChannel();ByteBuffer buffer = ByteBuffer.allocate(PAGE_SIZE);int checkSum = 0;while (-1 != (channel.read(buffer))){buffer.flip();while (buffer.hasRemaining()){checkSum += buffer.get();}buffer.clear();}return checkSum;}},new PerfTestCase("MemoryMappedFile"){public int testWrite(final String fileName) throws Exception{FileChannel channel = new RandomAccessFile(fileName, "rw").getChannel();MappedByteBuffer buffer = channel.map(READ_WRITE, 0,Math.min(channel.size(), MAX_VALUE));int checkSum = 0;for (long i = 0; i < FILE_SIZE; i++){if (!buffer.hasRemaining()){buffer = channel.map(READ_WRITE, i,Math.min(channel.size() - i , MAX_VALUE));}byte b = (byte)i;checkSum += b;buffer.put(b);}channel.close();return checkSum;}public int testRead(final String fileName) throws Exception{FileChannel channel = new RandomAccessFile(fileName, "rw").getChannel();MappedByteBuffer buffer = channel.map(READ_ONLY, 0,Math.min(channel.size(), MAX_VALUE));int checkSum = 0;for (long i = 0; i < FILE_SIZE; i++){if (!buffer.hasRemaining()){buffer = channel.map(READ_WRITE, i,Math.min(channel.size() - i , MAX_VALUE));}checkSum += buffer.get();}channel.close();return checkSum;}},};
}
结果
400MB file
===========
RandomAccessFile write=379,610,750 read=1,452,482,269 bytes/sec
BufferedStreamFile写入= 98,178,331读取= 286,433,566字节/秒
BufferedStreamFile写入= 100,244,738读取= 288,857,545字节/秒
BufferedStreamFile写入= 82,948,562读取= 154,100,827字节/秒 BufferedStreamFile写入= 108,503,311读取= 153,869,271字节/秒 BufferedStreamFile写入= 113,055,478读取= 152,608,047字节/秒
BufferedChannelFile写入= 228,443,948读取= 356,173,913字节/秒
BufferedChannelFile写入= 265,629,053读取= 374,063,926字节/秒
BufferedChannelFile写= 223,825,136读= 1,539,849,624字节/秒BufferedChannelFile写= 232,992,036读= 1,539,849,624字节/秒BufferedChannelFile写= 212,779,220读= 1,534,082,397字节/秒 MemoryMappedFile写入= 300,955,180读取= 305,899,925字节/秒 MemoryMappedFile写入= 313,149,847读取= 310,538,286字节/秒 MemoryMappedFile写入= 326,374,501读取= 303,857,566字节/秒 MemoryMappedFile写入= 327,680,000读取= 304,535,315字节/秒 MemoryMappedFile写入= 326895450读取= 303632320字节/秒
8GB文件
============
RandomAccessFile写入= 167,402,321读取= 251,922,012字节/秒 RandomAccessFile写入= 193,934,802读取= 257,052,307字节/秒 RandomAccessFile写入= 192,948,159读取= 248,460,768字节/秒 RandomAccessFile写入= 191,814,180读取= 245,225,408字节/秒 RandomAccessFile写入= 190,635,762读取= 275,315,073字节/秒
BufferedStreamFile写入= 154,823,102读取= 248,355,313字节/秒
BufferedStreamFile写入= 152,083,913读取= 253,418,301字节/秒
BufferedStreamFile写入= 133,099,369读取= 146,056,197字节/秒 BufferedStreamFile write = 131,065,708 read = 146,217,827字节/秒 BufferedStreamFile写入= 132694052读取= 148116004字节/秒
BufferedChannelFile写入= 186,703,740读取= 215,075,218字节/秒
BufferedChannelFile写入= 190,591,410读取= 211,030,680字节/秒BufferedChannelFile写入= 187,220,038读取= 223,087,606字节/秒
BufferedChannelFile写入= 191,585,397读取= 221,297,747字节/秒 BufferedChannelFile写入= 192,653,214读取= 211,789,038字节/秒
MemoryMappedFile写入= 123,023,322读取= 231,530,156字节/秒
MemoryMappedFile写入= 121,961,023读取= 230,403,600字节/秒
MemoryMappedFile写入= 123,317,778读取= 229,899,250字节/秒 MemoryMappedFile写入= 121,472,738读取= 231,739,745字节/秒 MemoryMappedFile写入= 120,362,615读取= 231,190,382字节/秒
分析
翻译自: https://www.javacodegeeks.com/2012/07/java-sequential-io-performance.html
java io顺序
java io顺序_Java顺序IO性能相关推荐
- java 流压缩_java之IO流:压缩文件
在java的学习中,IO流是一个避不开的话题.在java日常的学习和使用中,为了给不同的场景提供不同的用途,IO流拥有一个庞大的方法集合.虽然给使用者提供了极大的便利,但此同时也带来了学习时的异常冗杂 ...
- java io 结构_java 的IO类库的基本架构
java IO 操作的类在java.io 包中,大概可以分为以下几组: 基于字节操作的接口:InputStream 和 OutputStream 基于字符操作的接口:Writer 和 Reader 基 ...
- java写入简介_Java关于IO流的介绍
2.InputStream 和 OutputStream 是两个 abstact 类,对于字节为导向的 stream 都扩展这两个基类; inputStream中的关键方法: read():方法是读取 ...
- java io操作_Java 的IO操作(文件的读,写操作)
/* * FileEditor.java * * Created on 2006年9月13日, 下午2:22 * * To change this template, choose Tool ...
- java音频剪切_Java使用IO流实现音频的剪切和拼接
需求:使用IO流将指定目录下的若干个音频文件的高潮部分,进行剪切,并重新拼接成一首新的音频文件 思路(以两首歌为例): 第一首歌有一个输入流对象bis1.第二首歌有一个输入流对象bis2,他们公用一条 ...
- java io处理_java之IO处理
一.Java的 IO 处理 1. input output 输入和输出 2. 输入? --学习对于学生来说 -- 由外面的信息到大脑 -- 由外存到内存 3. 输出? -- 授课对于老师来说 -- 由 ...
- java 文件io操作_java文件IO操作。
public static void main(String[] args) { String filename = "F:/zhangming_test/test.txt"; S ...
- java uuid 效率_java uuid第一次性能
在java中产生uuid的方式是使用java.util.UUID. UUID.randomUUID().toString(); 我在测试redis性能时,使用uuid产生测试数据,发现多线程测试red ...
- java 对比工具_Java几款性能分析工具的对比
在给客户进行应用程序维护的过程中,我注意到一些关于高负载条件下的性能问题.理论上,增加对应用程序的负载会使性能有所下降,但是我认为性能下降的比率远远高于负载的增加,同时我也发现,性能可以通过改变应用程 ...
最新文章
- PHP如何调取vlc播放rtsp,H5+VLC播放RTSP视频流
- SpringAOP 相关知识点总结
- Python网络编程:IO多路复用
- python刷b站教程_python + selenium 刷B站播放量的实例代码
- 龙芯.NET正式发布 稳步推进生态建设
- selenium抓取_使用Selenium的网络抓取电子商务网站
- c语言第十章课后作业答案,C语言练习题第十章
- 合成器插件Sylenth1 2.2.1绿化版亲测有效
- 计算机专业英语课程内容,《计算机专业英语》课程教学大纲
- python矩阵乘法分治算法_详解矩阵乘法中的Strassen算法
- 颜色透明度16进制对照表
- 好文推荐(十六)——Git使用详细教程
- 学习经历与求职经历分享
- 淘宝产品3d展示-家具沙发三维模型应用
- 推荐 :数据可视化与信息可视化浅谈
- note:记各种资源
- MATLAB计算与常用命令
- 诚之和:波司登羽绒服都上万了 “土味羽绒服”高溢价引争议
- 原码、反码、补码二进制表示
- 【淘宝开店教程】淘宝直通车常见问题讲解
热门文章
- 架构师必须掌握的各种编码:ASCII、ISO-8859-1、GB2312
- Jodd - Java界的瑞士军刀轻量级工具包
- (一)Web Service——基本概念
- 新闻发布项目——访问温馨提示
- 整合MyBatis---SpringBoot
- RPC远程过程调用之 RMI实现
- android微信分享之创建工程以及启动微信
- java按列读取数据再存储_Java指定行读写数据
- (转)mybatis热部署加载*Mapper.xml文件,手动刷新*Mapper.xml文件
- no [query] registered for [filtered] in es7 的解决方法