MappedbyteBuffer与普通的IO性能比较


最近在看NIO部分,发现基于byteBuffer的MappedbyteBuffer性能比普通的stream在读取文件方面性能强很多。如下:

package com.demo.iodemo.bytebuffer;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

/**
 * Created with IntelliJ IDEA.
 * User: a549238
 * Date: 12/6/13
 * Time: 3:30 PM
 * To change this template use File | Settings | File Templates.
 */
public class MappedbyteBuffer {

    private static final int BYTE_SIZE = 4000000;
    private static final int INT_SIZE = 1000000;
    private static final int INT_BUFF_SIZE = 200000;

    private static final String FILE_NAME = "temp.txt";
    private static abstract class Tester
    {
        private String name ;
        public Tester(String name ){ this.name = name;}
        public void runTest()
        {
            System.out.print("name:" +name);
            long  start =System.nanoTime();
            try {
                test();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            System.out.format("    %.2f \n",(System.nanoTime()-start)/1.0e9);
        }
        public abstract void  test() throws IOException;
    }

    private static Tester[] tests ={
            new Tester("Stream Writer") {
                @Override
                public void test() throws IOException{
                    DataOutputStream out =new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(FILE_NAME))));
                    for(int i =0;i<BYTE_SIZE;i++)
                        out.write(i);
                    out.close();
                }
            },
            new Tester("Mapped Writer") {
                @Override
                public void test() throws IOException {
                    FileChannel fc= new RandomAccessFile(FILE_NAME,"rw").getChannel();
                    IntBuffer intBuffer = fc.map(FileChannel.MapMode.READ_WRITE,0,fc.size()).asIntBuffer();
                    for(int i=0;i<INT_SIZE;i++)
                    {
                        intBuffer.put(i);
                    }
                    fc.close();
                }
            },
            new Tester("Channel Writer WithOut Mapped") {
                @Override
                public void test() throws IOException {
                    FileChannel fc= new RandomAccessFile(FILE_NAME,"rw").getChannel();
                    ByteBuffer byteBuffer =ByteBuffer.allocate((int)fc.size());
                    //IntBuffer intBuffer = fc.map(FileChannel.MapMode.READ_WRITE,0,fc.size()).asIntBuffer();
                    for(int i=0;i<INT_SIZE;i++)
                    {
                        byteBuffer.put("12".getBytes());

                    }
                    byteBuffer.flip();
                    fc.write(byteBuffer);
                    fc.close();
                }
            },
            new Tester("Stream Read") {
                @Override
                public void test() throws IOException {
                     DataInputStream in =new DataInputStream(new BufferedInputStream(new FileInputStream(FILE_NAME)));
                    for(int i =0 ;i<INT_SIZE;i++)
                        in.readInt();
                    in.close();
                }
            },
            new Tester("Mapped Read") {
                @Override
                public void test() throws IOException {
                    FileChannel fc= new FileInputStream(new File(FILE_NAME)).getChannel();
                    IntBuffer intBuffer =fc.map(FileChannel.MapMode.READ_ONLY,0,fc.size()).asIntBuffer();
                    while(intBuffer.hasRemaining())
                        intBuffer.get();
                    fc.close();
                }
            },
            new Tester("Stream Read/Write") {
                @Override
                public void test() throws IOException {
                    RandomAccessFile file =new RandomAccessFile(FILE_NAME,"rw");
                    file.writeInt(1);
                    for(int i =0;i<INT_BUFF_SIZE;i++)
                    {
                        file.seek(file.length()-4);
                        file.writeInt(file.readInt());
                    }
                    file.close();
                }
            } ,
            new Tester("Mapped Read/write") {
                @Override
                public void test() throws IOException {
                    FileChannel fc= new RandomAccessFile(new File(FILE_NAME),"rw").getChannel();
                    IntBuffer intBuffer =fc.map(FileChannel.MapMode.READ_WRITE,0,fc.size()).asIntBuffer();
                    intBuffer.put(0);
                    for(int i=1;i<INT_BUFF_SIZE;i++)
                        intBuffer.put(intBuffer.get(i-1));
                    fc.close();
                }
            }
    };

    public static void main(String[] args)
    {
        for(Tester tester: tests)
            tester.runTest();
        //tests[1].runTest();
    }
}
/*
name:Stream Writer0.16
name:Mapped Writer0.01
name:Stream Read0.10
name:Mapped Read0.01
name:Stream Read/Write2.37
name:Mapped Read/write0.01
 */

out:

name:Stream Writer    0.16 
name:Mapped Writer    0.06 
name:Channel Writer WithOut Mapped    0.19 
name:Stream Read    0.10 
name:Mapped Read    0.01 
name:Stream Read/Write    2.29 
name:Mapped Read/write    0.01 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值