FileChannel、FileInputStream性能对比

一、性能对比

方法50M * 10230M * 101G * 101.5G * 102G * 1013G * 10
nioTransferFrom()664 ms2,191ms9,826ms28,25632,195ms266,417ms
nioTransferTo()592 ms1,956ms9,834ms28,471ms32,427ms295,478ms
nioByteBuffer()698ms2,611ms10,474ms15,033ms25,095ms231,016ms
ioFileInputStream()1,055ms3,638ms10,474ms15,033ms25,095ms213,016
ioBuffered()1,100ms3,485ms14,119ms18,944ms26,274ms220,801ms

二、测试代码

public static void nioTransferFrom(String inFile, String outFile) throws Exception {
	long start = System.currentTimeMillis();
	RandomAccessFile fromFile = new RandomAccessFile(inFile, "rw");
	FileChannel inChannel = fromFile.getChannel();
	
	RandomAccessFile toFile = new RandomAccessFile(outFile, "rw");
	FileChannel outChannel = toFile.getChannel();
	
	long position = 0;
	long count = inChannel.size();
	inChannel.transferFrom(position, count, outChannel);

	outChannel.close();
	toFile.close();
	inChannel.close();
	fromFile.close();

	long end = System.currentTimeMillis();
	System.out.println("Time taken: " + (start - end) + " ms");
}
public static void nioTransferTo(String inFile, String outFile) throws Exception {
	long start = System.currentTimeMillis();
	RandomAccessFile fromFile = new RandomAccessFile(inFile, "rw");
	FileChannel inChannel = fromFile.getChannel();
	
	RandomAccessFile toFile = new RandomAccessFile(outFile, "rw");
	FileChannel outChannel = toFile.getChannel();
	
	long position = 0;
	long count = inChannel.size();
	
	while(count > 0){
		long current = outChannel.transferTo(position, count, inChannel);
		if(current > 0) {
			position += current;
			count -= current;
		}
	}

	outChannel.close();
	toFile.close();
	inChannel.close();
	fromFile.close();

	long end = System.currentTimeMillis();
	System.out.println("Time taken: " + (start - end) + " ms");
}
public static void nioByteBuffer(String inFile, String outFile) throws Exception {
    long start = System.currentTimeMillis();
    FileInputStream in = new FileInputStream(inFile);
    FileOutputStream out = new FileOutputStream(outFile);
    
    FileChannel inChannel = in.getChannel();
    FileChannel outChannel = out.getChannel();

    ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 1024 * 8);
    while(inChannel.read(buf) != -1) {
        buf.flip();
        outChannel.write(buf);
        buf.clear();
    }

    outChannel.close();
    inChannel.close();

	long end = System.currentTimeMillis();
	System.out.println("Time taken: "+(start - end)+" ms");
}
public static void ioFileInputStream(String inFile, String outFile) throws Exception {
    long start= System.currentTimeMillis();
    FileInputStream in = new FileInputStream(inFile);
    FileOutputStream out = new FileOutputStream(outFile);
    
    byte[] buf = new byte[1024 * 1024 * 8];
    while(in.read(buf) != -1) {
        out.write(buf, 0, len);
    }
    out.flush();
    out.close();
    in.close();
    long end = System.currentTimeMillis();
    System.out.println("Time taken: " + (end - start) + " ms");
}
public static void ioBuffered(String inFile, String outFile) throws Exception {
    long start= System.currentTimeMillis();
    FileInputStream inStream = new FileInputStream(inFile);
    FileOutputStream outStream = new FileOutputStream(outFile);
    
	int bufferSize = 1024 * 1024 * 8;
	BufferedFileInputStream in = new BufferedFileInputStream(inStream, bufferSize);
	BufferedFileOutputStream out = new BufferedFileOutputStream(outStream, bufferSize);
	
    byte[] buf = new byte[bufferSize];
    while(in.read(buf) != -1) {
        out.write(buf, 0, len);
    }
    out.flush();
    out.close();
    in.close();
    long end = System.currentTimeMillis();
    System.out.println("Time taken: " + (end - start) + " ms");
}

三、单线程测试

 public static void main(String[] args) throws Exception {
  	  String inFile = "xxx.rar";
  	  String outFile;
  	  long start = System.currentTimeMillis();
  	  for(int i = 0; i < 10; i++) {
		outFile = "xxx" + i + ".rar";
		nioTransferFrom(inFile, outFile);
		// nioTransferTo(inFile, outFile);
		// nioByteBuffer(inFile, outFile);
		// ioFileInputStream(inFile, outFile);
		// ioBuffered(inFile, outFile);
 	}
	long end = System.currentTimeMillis();
	System.out.println("Sum: " + (start - end) + " ms");
 }

四、多线程测试

public static void main(String[] args) throws Exception {
	 final String inFile = "xxx.rar";
     final CountDownLatch startGate = new CountDownLatch(1);
     final CountDownLatch endCate  = new CountDownLatch(10);
	 for(int i = 0; i < 10; i++) {
            Thread t = new Thread(){
            	@Override
                public void run(){
                    try {
                        startGate.await();
                        String outFile = "xxx" + UUID.randomUUID() + ".rar";
                        nioTransferFrom(inFile, outFile);
						// nioTransferTo(inFile, outFile);
						// nioByteBuffer(inFile, outFile);
						// ioFileInputStream(inFile, outFile);
						// ioBuffered(inFile, outFile);
                    } catch (Throwable e) {
                        e.printStackTrace();
                    } finally {
                        endCate.countDown();
                    }
                }
            };
            t.start();
        }
        
    long start = System.currentTimeMillis();
	System.out.println("Wwait:" + start);
	startGate.countDown(); 
	endCate.await();
	
	long end = System.currentTimeMillis();
	System.out.println("Done: " + (end - start) + " ms");
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

书香水墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值