How can I speed up line by line reading of an ASCII file? (C++)

前端 未结 9 974
夕颜
夕颜 2020-12-30 08:02

Here\'s a bit of code that is a considerable bottleneck after doing some measuring:

//-----------------------------------------------------------------------         


        
9条回答
  •  灰色年华
    2020-12-30 08:36

    Unfortunately, there's not much you can do to increase performance when using an fstream.

    You may be able to get a very slight speed improvement by reading in larger chunks of the file and then parsing out single words, but this depends on how your fstream implementation does buffering.

    The only way to get a big improvement is to use your OS's I/O functions. For example, on Windows, opening the file with the FILE_FLAG_SEQUENTIAL_SCAN flag may speed up reads, as well as using asynchronous reads to grab data from disk and parse it in parallel.

提交回复
热议问题