写运营成本 [英] Write operation cost

查看:87
本文介绍了写运营成本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Go程序,可以将字符串写入文件中.我有一个循环,该循环 20000次,并且在每次迭代中,我都会将大约20-30个字符串写入一个文件中.我只是想知道哪种是将其写入文件的最佳方法.

I have a Go program which writes strings into a file.I have a loop which is iterated 20000 times and in each iteration i am writing around 20-30 strings into a file. I just wanted to know which is the best way to write it into a file.

  • 方法1:在代码开头始终打开文件指针,然后 为每个字符串写它.使其具有20000 * 30个写操作.

  • Approach 1: Keep open the file pointer at the start of the code and write it for every string. It makes it 20000*30 write operations.

方法2:使用bytes.Buffer去并将所有内容存储在缓冲区中,然后 最后写它.在这种情况下,文件指针也应该是 从代码开头或代码结尾打开.做 有关系吗?

Approach 2: Use bytes.Buffer Go and store everything in the buffer and write it at the end.Also in this case should the file pointer be opened from the beginning of the code or at the end of the code. Does it matter?

我认为方法2应该更好.有人可以有理由确认这一点. 一次写作要比定期写作更好.因为文件指针始终会打开. 我正在使用f.WriteString(<string>),并且buffer.WriteString(<some string>)缓冲区的类型为bytes.Buffer,而f是打开的文件指针.

I am assuming approach 2 should work better. Can someone confirm this with a reason. How does writing at once be better than writing periodically. Because the file pointer will anyways be open. I am using f.WriteString(<string>) and buffer.WriteString(<some string>) buffer is of type bytes.Buffer and f is the file pointer open.

推荐答案

bufio 包已完全创建为此任务. bufio.Writer不是对每个Write调用进行syscall,而是在进行syscall之前在内部存储器中缓冲多达固定数量的字节.进行系统调用后,内部缓冲区将重新用于下一部分数据

bufio package has been created exactly for this kind of task. Instead of making a syscall for each Write call bufio.Writer buffers up to a fixed number of bytes in the internal memory before making a syscall. After a syscall the internal buffer is reused for the next portion of data

与第二种方法相比bufio.Writer

  • 进行更多的系统调用(N/S而不是1)
  • 使用更少的内存(S个字节而不是N个字节)
  • makes more syscalls (N/S instead of 1)
  • uses less memory (S bytes instead of N bytes)

其中S-是缓冲区大小(可以通过bufio.NewWriterSize指定),N-需要写入的数据总大小.

where S - is buffer size (can be specified via bufio.NewWriterSize), N - total size of data that needs to be written.

用法示例( https://play.golang.org/p/AvBE1d6wpT ):

f, err := os.Create("file.txt")
if err != nil {
    log.Fatal(err)
}
defer f.Close()

w := bufio.NewWriter(f)
fmt.Fprint(w, "Hello, ")
fmt.Fprint(w, "world!")
err = w.Flush() // Don't forget to flush!
if err != nil {
    log.Fatal(err)
}

这篇关于写运营成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆