[筆記]Go語言寫檔案幾種方式效能對比

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

Go語言中寫檔案有多種方式,這裡進行如下幾種方式的速度對比:

  1. 開啟檔案,寫入內容,關閉檔案。如此重複多次
  2. 開啟檔案,寫入內容,defer 關閉檔案。如此重複多次
  3. 開啟檔案,重複多次寫入內容,defer 關閉檔案

在VMWare下的Ubuntu 14.04下啟動並執行結果表明:

  • 方式1速度最慢,但是慢的很穩定
  • 方式2比方式1略快,但是重複次數多了後會報錯,應該是defer被壓棧太多導致系統撐不了太多開啟的檔案
  • 方式3速度約是前兩者的2倍,因為減少了很多開啟關閉檔案的操作

測試代碼如下:

package mainimport (    "fmt"    "os"    "time")func benchmarkFileWrite(filename string, n int, index int) (d time.Duration) {    v := "ni shuo wo shi bu shi tai wu liao le a?"    os.Remove(filename)    t0 := time.Now()    switch index {    case 0: // open file and write, then close, repeat n times        for i := 0; i < n; i++ {            file, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)            if err != nil {                fmt.Println(index, i, "open file failed.", err.Error())                break            }            file.WriteString(v)            file.WriteString("\n")            file.Close()        }    case 1: // open file and write, defer close, repeat n times        for i := 0; i < n; i++ {            file, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)            if err != nil {                fmt.Println(index, i, "open file failed.", err.Error())                break            }            defer file.Close()            file.WriteString(v)            file.WriteString("\n")        }    case 2: // open file and write n times, then close file        file, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)        if err != nil {            fmt.Println(index, "open file failed.", err.Error())            break        }        defer file.Close()        for i := 0; i < n; i++ {            file.WriteString(v)            file.WriteString("\n")        }    }    t1 := time.Now()    d = t1.Sub(t0)    fmt.Printf("time of way(%d)=%v\n", index, d)    return d}func main() {    const k, n int = 3, 1000    d := [k]time.Duration{}    for i := 0; i < k; i++ {        d[i] = benchmarkFileWrite("benchmarkFile.txt", n, i)    }    for i := 0; i < k-1; i++ {        fmt.Printf("way %d cost time is %6.1f times of way %d\n", i, float32(d[i])/float32(d[k-1]), k-1)    }}

 

當n=1000時,測試結果如下

  time of way(0)=38.719386ms  time of way(1)=31.428677ms  time of way(2)=17.930829ms  way 0 cost time is    2.2 times of way 2  way 1 cost time is    1.8 times of way 2

 

當n=5000時,測試結果如下(因為方式1報錯,所以它的時間是錯的)

  time of way(0)=170.003521ms  1 1021 open file failed. open benchmarkFile.txt: too many open files  time of way(1)=32.388994ms  time of way(2)=77.777936ms  way 0 cost time is    2.2 times of way 2  way 1 cost time is    0.4 times of way 2

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.