几种file copy的性能对比

14 篇文章 0 订阅

测试了几种file copy的性能:

package mytest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;

import org.apache.commons.io.IOUtils;

import com.taobao.hsf.tlog.proxy.appender.LogAppender;
import com.taobao.hsf.tlog.proxy.appender.impl.RollingFileAppender;

public class MyTest {

	static BufferedReader read() throws FileNotFoundException {
		InputStream input = new FileInputStream(
				"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/input");
		InputStreamReader reader = new InputStreamReader(input);
		BufferedReader bufferedReader = new BufferedReader(reader);
		return bufferedReader;
	}

	static BufferedInputStream input() throws FileNotFoundException {
		InputStream input = new FileInputStream(
				"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/input");
		return new BufferedInputStream(input);
	}

	static FileInputStream inputFile() throws FileNotFoundException {
		FileInputStream input = new FileInputStream(
				"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/input");
		return input;
	}

	static void test1() throws IOException {
		BufferedReader bufferedReader = read();
		long begin = System.currentTimeMillis();
		FileOutputStream ostream = new FileOutputStream(
				"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/output1");
		BufferedOutputStream bos = new BufferedOutputStream(ostream, 8192);
		String line;
		while ((line = bufferedReader.readLine()) != null) {
			line += "\n";
			bos.write(line.getBytes("utf-8"));
		}
		bos.flush();
		long end = System.currentTimeMillis();
		System.out.println("buffered stream cost: " + (end - begin) + " ms");
		bufferedReader.close();
	}

	static void test2() throws IOException {
		BufferedReader bufferedReader = read();
		long begin = System.currentTimeMillis();
		String line;
		LogAppender newAppender = new RollingFileAppender(
				"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/output2", 1024 * 1024 * 100, false);
		while ((line = bufferedReader.readLine()) != null) {
			line += "\n";
			newAppender.append(line);
		}
		newAppender.flush();
		long end = System.currentTimeMillis();
		System.out.println("RollingFileAppender cost: " + (end - begin) + " ms");
		bufferedReader.close();
	}

	static void test3() throws IOException {
		BufferedReader bufferedReader = read();
		long begin = System.currentTimeMillis();
		String line;
		while ((line = bufferedReader.readLine()) != null) {
			line += "\n";
		}
		long end = System.currentTimeMillis();
		System.out.println("only read cost: " + (end - begin) + " ms");
	}

	static void test4() throws IOException {
		BufferedReader bufferedReader = read();
		long begin = System.currentTimeMillis();
		FileOutputStream ostream = new FileOutputStream(
				"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/output3");
		IOUtils.copy(bufferedReader, ostream);
		long end = System.currentTimeMillis();
		System.out.println("a.c.o.1 : " + (end - begin) + " ms");
	}

	static void test5() throws IOException {
		InputStream bufferedReader = input();
		long begin = System.currentTimeMillis();
		FileOutputStream ostream = new FileOutputStream(
				"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/output4");
		IOUtils.copy(bufferedReader, ostream);
		long end = System.currentTimeMillis();
		System.out.println("a.c.o.2 : " + (end - begin) + " ms");

	}

	static void test6() throws IOException {
		FileChannel input = inputFile().getChannel();
		long begin = System.currentTimeMillis();
		FileChannel out = new
				FileOutputStream("/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/output5")
						.getChannel();
		input.transferTo(0, input.size(), out);
		long end = System.currentTimeMillis();
		System.out.println("chanel transfer : " + (end - begin) + " ms");
		input.close();
		out.close();

	}

	public static void main(String[] args) throws IOException {

		for (int i = 0; i < 100; ++i) {
			test1();
			test2();
			test3();
			test4();
			test5();
			test6();
			System.out.println("-------------------------");
		}
	}
}

 

 

测试结果:

写道
-------------------------
buffered stream cost: 110 ms
RollingFileAppender cost: 134 ms
only read cost: 54 ms
a.c.o.1 : 85 ms
a.c.o.2 : 55 ms
chanel transfer : 10 ms
-------------------------
buffered stream cost: 131 ms
RollingFileAppender cost: 137 ms
only read cost: 55 ms
a.c.o.1 : 175 ms
a.c.o.2 : 21 ms
chanel transfer : 10 ms
-------------------------
buffered stream cost: 121 ms
RollingFileAppender cost: 122 ms
only read cost: 56 ms
a.c.o.1 : 161 ms
a.c.o.2 : 31 ms
chanel transfer : 14 ms
-------------------------
buffered stream cost: 121 ms
RollingFileAppender cost: 120 ms
only read cost: 55 ms
a.c.o.1 : 89 ms

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值