NIO(JSR 51 NIO.1)的性能测试
2013-12-02 19:41
337 查看
原来的io已经用新io重新实现过。对于读写文件,看到很多人都推荐使用new io,说什么速度快。我做一下测试。
本测试读写的文件的大小是2M字节多一点。
测试结论:只要缓冲大小设置得当,使用旧io和基本类型写的IO是最快的。
本测试读写的文件的大小是2M字节多一点。
import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.file.Paths; /** * @ 测试结论:只要缓冲大小设置得当,使用旧io和基本类型写的IO是最快的。 * @author jdgdf566 */ public class NewMain { private static int commonIObuffer = 1024 * 32; /** * nio内存映射 * * @throws IOException */ public static void mapChannel() throws IOException { FileInputStream in = new FileInputStream("Java NIO (JSR 51 NIO.1)中文版.pdf"); long size = in.available(); RandomAccessFile out = new RandomAccessFile(Double.toString(Math.random()), "rw"); FileChannel inc = in.getChannel(); MappedByteBuffer bf = inc.map(FileChannel.MapMode.READ_ONLY, 0, size); FileChannel outc = out.getChannel(); MappedByteBuffer outbf = outc.map(FileChannel.MapMode.READ_WRITE, 0, size); outbf.put(bf); inc.close(); outc.close(); in.close(); out.close(); } /** * 原io * * @throws IOException */ public static void commonIO() throws Exception { NewMain.fileToStream("Java NIO (JSR 51 NIO.1)中文版.pdf", new FileOutputStream(Double.toString(Math.random()))); } public static int fileToStream(String path, OutputStream out) throws Exception { int bufferSize = commonIObuffer; FileInputStream fis = new FileInputStream(path); //ba文件写入liu int dataSize = 0; int len; byte[] bs = new byte[bufferSize]; while ((len = fis.read(bs)) != -1) { out.write(bs, 0, len); dataSize += len; } out.flush(); return dataSize; } /** * nio * * @throws IOException */ public static void channel_transferTo() throws Exception { String path = "F:\\NetBeansProjects\\JavaApplicationTest\\src\\Java NIO (JSR 51 NIO.1)中文版.pdf"; //String outpath = "F:\\NetBeansProjects\\JavaApplicationTest\\src/0.2974592999005251"; FileChannel fileChannel = FileChannel.open(Paths.get(path)); fileChannel.transferTo(0, fileChannel.size(), Channels.newChannel(new BufferedOutputStream(new FileOutputStream(Double.toString(Math.random())), commonIObuffer))); } /** * @param args the command line arguments */ public static void main(String[] args) throws Exception { // TODO code application logic here long t1 = System.currentTimeMillis(); //NewMain.mapChannel(); //NewMain.commonIO(); //NewMain.channel_transferTo(); long t2 = System.currentTimeMillis(); System.out.println(t2 - t1); } }
测试结论:只要缓冲大小设置得当,使用旧io和基本类型写的IO是最快的。
相关文章推荐
- java对比IO和NIO的文件读写性能测试
- tomcat bio nio apr 模式性能测试与个人看法
- tomcat bio nio apr 模式性能测试
- tomcat bio nio apr 模式性能测试与个人看法
- tomcat bio nio apr 模式性能测试与个人看法
- 一次诡异的NIO和IO性能测试
- java aio tomcat bio nio apr 模式性能测试
- ab测试tomcat并发性能(测试BIO与NIO差别)(一)
- tomcat-tomcat bio nio apr 模式性能测试与个人看法
- java对比IO和NIO的文件读写性能测试
- Nio中直接缓冲区与间接缓冲区性能测试
- dd测试硬盘性能
- 数据库性能测试方案示例
- C-NCAP和性能测试
- 软件测试中的性能测试、负载测试、压力测试
- NoSQL性能测试:MongoDB VS SequoiaDB
- cxx11之atomic、lock、lock-guard性能测试比较
- 关于node.js的web框架的微信h5牌九平台开发及并发性能测试
- 虚拟机性能测试:三 物理机(宿主机)性能
- 【性能测试报告-2016.08.02】