Go_VS_Java_檔案按行讀寫速度

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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 mainimport ("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 stringfor {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)}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.