Why are std::fstreams so slow?

前端 未结 5 850
不思量自难忘°
不思量自难忘° 2021-01-30 20:49

I was working on a simple parser and when profiling I observed the bottleneck is in... file read! I extracted very simple test to compare the performance of fstreams

5条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 21:07

    TL;DR: Try adding this to your code before doing the writing:

    const size_t bufsize = 256*1024;
    char buf[bufsize];
    mystream.rdbuf()->pubsetbuf(buf, bufsize);
    

    When working with large files with fstream, make sure to use a stream buffer.

    Counterintuitively, disabling stream buffering dramatically reduces performance. At least the MSVC implementation copies 1 char at a time to the filebuf when no buffer was set (see streambuf::xsputn()), which can make your application CPU-bound, which will result in lower I/O rates.

    NB: You can find a complete sample application here.

提交回复
热议问题