`
san_yun
  • 浏览: 2595025 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

几种file copy的性能对比

 
阅读更多

测试了几种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

 

分享到:
评论

相关推荐

    Linux操作系统基础教程

    几种重要的配置文件.......................................................................................................26 三.Linux下常用的工具软件......................................................

    嵌入式红绿灯控制系统

    本文介绍了一种城市十字路口交通信号灯控制系统。该系统采用了以8051为内核的单片机芯片AT89s51作为核心控制器,以嵌入式操作系统RTX51为软件开发平台,通过控制城市十字路口的交通信号灯来指挥交通。该系统具有制作...

    网管教程 从入门到精通软件篇.txt

     注意:使用 bootcfg /rebuild 之前,应先通过 bootcfg /copy 命令备份 boot.ini 文件。  bootcfg /scan 扫描用于 Windows 安装的所有磁盘并显示结果。  注意:这些结果被静态存储,并用于本次会话。如果在...

    一个进程池的服务器程序

    * Attach file descriptors 0, 1, and 2 to /dev/null. */ int fd0, fd1, fd2; fd0 = open("/dev/null", O_RDWR); fd1 = dup(0); fd2 = dup(0); if (fd0 != 0 || fd1 != 1 || fd2 != 2) { printf("init ...

    华为编程开发规范与案例

    仔细比较两种数据的差别,发现出现主机复位问题的数据中DSL板配置了MNT/MLT端口,但是没有做DSL端口之间的半永久数据。 于是在程序中不断加打印语句,通过后台的DBWIN调试程序跟踪,最后终于定位为:每当执行到...

    (重要)AIX command 使用总结.txt

    tcopy: Tape File: 1; Records: 1 to 251; Size: 2097152. ---磁带机文件头大小 tcopy: Tape File: 1; Record: 252; Size 344064. ---磁带机文件头大小 tcopy: File: 1; End of File after: 252 Records, 526729216 ...

    flash shiti

    37.比较运算符分为哪几种? A. 数值比较运算符 B. 字串比较运算符 C. 字母比较运算符 D. 汉字比较运算符 38.在声音同步类型中包括哪几种类型? A. Event B. Start C. Stop D. Stream 39.以下操作哪几项...

    测试培训教材

    重命名需求项Cruise Reservation_Copy_1为Hotel Reservation 移动需求项到Reservations Management下 删除Hotel Reservation 把需求项转换成测试计划 选中Cruise Reservation 选择菜单“需求-&gt;转换...

Global site tag (gtag.js) - Google Analytics