前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go语言写文件几种方式性能对比

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

作者头像
李海彬
发布2018-07-26 10:31:14
1.1K0
发布2018-07-26 10:31:14
举报
文章被收录于专栏:Golang语言社区Golang语言社区

社区订阅号:Golang语言社区 社区服务号:Golang技术社区 如有问题或建议,请公众号留言;社区Leaf实战服务器开发火热报名中

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

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

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

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

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

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

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

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

测试代码如下:

代码语言:javascript
复制
package main
import (    "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时,测试结果如下

代码语言:javascript
复制
  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报错,所以它的时间是错的)

代码语言:javascript
复制
  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

本文来自:博客园

感谢作者:journeyonmyway

查看原文:go语言写文件几种方式性能对比

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-04-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档