Go语言中写文件有多种方式,这里进行如下几种方式的速度对比:

    • 1)打开文件,写入内容,关闭文件。如此重复多次;

    • 2)打开文件,写入内容,defer关闭文件。如此重复多次;

    • 3)打开文件,重复多次写入内容,defer关闭文件;

    在VMWare下的Ubuntu 14.04下运行的结果表明:

    • 方式1速度最慢,但是慢的很稳定

    • 方式2比方式1略快,但是重复次数多了后会报错,应该是defer被压栈太多导致系统撑不了太多打开的文件

    • 方式3速度约是前两者的2倍,因为减少了很多打开关闭文件的操作

    测试代码如下:

    1. package main
    2. import ( "fmt"
    3. "os"
    4. "time")
    5. func benchmarkFileWrite(filename string, n int, index int) (d time.Duration) {
    6. v := "ni shuo wo shi bu shi tai wu liao le a?"
    7. os.Remove(filename)
    8. t0 := time.Now() switch index { case 0: // open file and write, then close, repeat n times
    9. for i := 0; i < n; i++ {
    10. file, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) if err != nil {
    11. fmt.Println(index, i, "open file failed.", err.Error()) break
    12. }
    13. file.WriteString(v)
    14. file.WriteString("\n")
    15. file.Close()
    16. } case 1: // open file and write, defer close, repeat n times
    17. for i := 0; i < n; i++ {
    18. file, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) if err != nil {
    19. fmt.Println(index, i, "open file failed.", err.Error()) break
    20. }
    21. defer file.Close()
    22. file.WriteString(v)
    23. file.WriteString("\n")
    24. } case 2: // open file and write n times, then close file
    25. file, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) if err != nil {
    26. fmt.Println(index, "open file failed.", err.Error()) break
    27. }
    28. defer file.Close() for i := 0; i < n; i++ {
    29. file.WriteString(v)
    30. file.WriteString("\n")
    31. }
    32. }
    33. t1 := time.Now()
    34. d = t1.Sub(t0)
    35. fmt.Printf("time of way(%d)=%v\n", index, d) return d
    36. }
    37. func main() { const k, n int = 3, 1000
    38. d := [k]time.Duration{} for i := 0; i < k; i++ {
    39. d[i] = benchmarkFileWrite("benchmarkFile.txt", n, i)
    40. } for i := 0; i < k-1; i++ {
    41. fmt.Printf("way %d cost time is %6.1f times of way %d\n", i, float32(d[i])/float32(d[k-1]), k-1)
    42. }
    43. }

    当n=1000时,测试结果如下

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

    当n=5000时,测试结果如下(因为方式1报错,所以它的时间是错的)

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

    Go语言写文件几种方式性能对比 - 图1