Why are std::fstreams so slow?

前端 未结 5 860
不思量自难忘°
不思量自难忘° 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:16

    In contrary to other answers, a big issue with large file reads comes from buffering by the C standard library. Try using low level read/write calls in large chunks (1024KB) and see the performance jump.

    File buffering by the C library is useful for reading or writing small chunks of data (smaller than disk block size).

    On Windows I got almost a 3x performance boost dropping file buffering when reading and writing raw video streams.

    I also opened the file using native OS (win32) API calls and told the OS not to cache the file as this involves yet another copy.

提交回复
热议问题