【问题标题】:Why write bytes to files is slow for Go, compared with C与 C 相比,为什么 Go 向文件写入字节很慢
【发布时间】:2019-12-08 14:19:33
【问题描述】:

我刚刚发现 Go 向文件写入字节很慢。

我想创建一个 10mb 的文件。 Go 大约需要 1 分钟,但在 C 语言中不到 5 秒。

这是 Go 代码:

package main

import (
    "fmt"
    "os"
)

func main() {
    f, _ := os.Create("./src/test/test.txt")
    count := int(1.024e7)
    for i := 0; i < count; i++ {
        f.Write([]byte{byte('a' + i%24)})
    }
    f.Close()
    fmt.Println("ok")
}

和C:

#include <stdio.h>

int main()
{
    FILE *fp=fopen("data.txt","w");
    int  size=1.024e7;
    for(int i=0;i<size;i++)
        putc('a'+i%24,fp);
    fclose(fp);
    printf("ok");
    return 0;
}

【问题讨论】:

  • Go 默认为无缓冲 I/O。因此,与默认情况下在 C 中获得的不同缓冲行为的迷宫不同,程序员可以选择要使用的缓冲系统。

标签: c go


【解决方案1】:

无意义的微基准会产生无意义的结果。


去:

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    f, _ := os.Create("tmp/sotest/test.txt")
    w := bufio.NewWriter(f)
    count := int(1.024e7)
    for i := 0; i < count; i++ {
        w.Write([]byte{byte('a' + i%24)})
    }
    w.Flush()
    f.Close()
    fmt.Println("ok")
}

输出:

ok
real    0m0.159s
user    0m0.160s
sys     0m0.004s

C:

#include <stdio.h>

int main()
{
    FILE *fp=fopen("data.txt","w");
    int  size=1.024e7;
    for(int i=0; i<size; i++)
        putc('a'+i%24,fp);
    fclose(fp);
    printf("ok");
    return 0;
}

输出:

ok
real    0m0.058s
user    0m0.045s
sys     0m0.004s

【讨论】:

  • 但是你知道为什么吗? C 实现是否使用开箱即用的缓冲?
  • @mh-cbon:是的,C stdio 流通常是行缓冲或完全缓冲的,具体取决于它们是连接到“终端式”设备还是“文件式”数据。但是,stderr 流通常是 un 缓冲的,无论其数据接收器如何。系统可能会选择提供额外的覆盖,例如魔术环境变量(尽管对于管道输出的情况,没有很好的方法来去缓冲或强制行缓冲标准输出)。
  • @mh-cbon,例如,见stdbuf
猜你喜欢
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
  • 2021-09-04
  • 2012-05-05
  • 1970-01-01
  • 2021-04-19
  • 2015-10-27
相关资源
最近更新 更多