Go_VS_Java_文件按行读写速度

1:运行环境说明:

go:1.0.3 64位

jdk:1.7_u9 64位

操作系统:win7 64位,Thinkpad T410i,i3 M350,2.27GHz,双核4线程,8G内存

文本文件:UTF-8纯文本文件,以行为单位,文件大小2.6G

程序目标:以行为单位读取该文件,并将读取的行写入另一个新的文件内。

2:程序结果

java  totalTime:96096毫秒  client模式
java  totalTime:77595毫秒  server模式
go     totalTime:75328毫秒
以上结果仅供参考...

3:测试代码

java代码

package function.filehelper;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class SingleFileMergeFilter3 {
	
	public String inFile = "D:/00/tk.txt";
	public String inEncode = "UTF-8";
	
	public String outFile = "D:/00/tk3.txt";
	public String outEncode = "UTF-8";
	
	private FileInputStream fis;
	private InputStreamReader isr;
	private BufferedReader bfr;
	
	private FileOutputStream fos;
	private OutputStreamWriter osw;
	private BufferedWriter bwo;
	
	public void dealByLine() throws Exception{
		this.fis = new FileInputStream(inFile);
		this.isr = new InputStreamReader(this.fis, inEncode);
		this.bfr = new BufferedReader(this.isr);
		
		this.fos = new FileOutputStream(outFile);
		this.osw = new OutputStreamWriter(this.fos, outEncode);
		this.bwo = new BufferedWriter(this.osw);
		
		long s = System.currentTimeMillis();
		String line = null;
		while((line = this.bfr.readLine()) != null){
			this.bwo.write(line + "\r\n");
		}
		
		this.bwo.flush();
		long e = System.currentTimeMillis();
		System.out.println("totalTime:" + (e - s));
		
		this.bfr.close();
		this.bwo.close();
	}
	
	public static void main(String[] args) throws Exception {
		new SingleFileMergeFilter3().dealByLine();
	}
}

Go代码
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"time"
)

func main() {
	var inFile = "D:/00/tk.txt"
	var outFile = "D:/00/tk2.txt"

	fi, _ := os.Open(inFile)
	defer fi.Close()

	fo, _ := os.Create(outFile)
	defer fo.Close()

	r := bufio.NewReader(fi)
	w := bufio.NewWriter(fo)

	var s = time.Now()
	var line string
	for {
		lineByte, _, er3 := r.ReadLine()
		if er3 != nil && er3 == io.EOF {
			break
		}

		line = string(lineByte)
		w.WriteString(line + "\r\n")
	}

	w.Flush()

	var e = time.Now()
	fmt.Println("totalTime:", e.Sub(s).Nanoseconds()/1000/1000)
}


你可能感兴趣的:(Go_VS_Java_文件按行读写速度)