Fastest way to read numerical values from text file in C++ (double in this case)

前端 未结 8 2059
误落风尘
误落风尘 2020-12-05 03:08

Currently, my code is simply this:

void ReadFile(double Cst[][1000], char* FileName, int height)

FILE* ifp;
double value;
int nRead = 0;
int mRead = 0;

//o         


        
8条回答
  •  隐瞒了意图╮
    2020-12-05 03:58

    A fast way is to allocate a text buffer or string, read as much as you can into the string, then parse the string.

    Your first bottleneck is file I/O. Second (in order) is converting text into numbers. You should profile your program as to whether sscanf or std::istringstream is faster. Modifications to the I/O portion will yield the biggest performance changes.

    To make the process even faster, using multiple threads and double buffering. One thread reads data into one or more buffers, while another thread parses data out of those buffers.

    Additional improvements can be made by changing the data to fixed size fields and records.

提交回复
热议问题