我正在尝试以更快的方式从文件中读取数据。目前我正在做的是以下,但对于大文件来说非常慢。我想知道是否有更快的方法来做到这一点?我需要存储一个结构体的值,我在下面定义了它。在C++中读取文件到内存的最快方法?
std::vector<matEntry> matEntries;
inputfileA.open(matrixAfilename.c_str());
// Read from file to continue setting up sparse matrix A
while (!inputfileA.eof()) {
// Read row, column, and value into vector
inputfileA >> (int) row; // row
inputfileA >> (int) col; // col
inputfileA >> val; // value
// Add row, column, and value entry to the matrix
matEntries.push_back(matEntry());
matEntries[index].row = row-1;
matEntries[index].col = col-1;
matEntries[index].val = val;
// Increment index
index++;
}
我的结构:
struct matEntry {
int row;
int col;
float val;
};
该文件格式如下(整型,整型,浮点):
1 2 7.9
4 5 9.008
6 3 7.89
10 4 10.21
更多信息:
- 我知道运行时文件中的行数。
- 我正面临瓶颈。分析器说while()循环是瓶颈。
**您是否面临瓶颈?**虽然['mmap'](https://en.wikipedia.org/wiki/Mmap)可能是另一种选择,这通常被认为是'最快'的方式,具体取决于你如何使用文件的内容。 – Qix
你的配置文件说哪个部分很慢?文件读取? 'vector'重新分配? 'fstream'操作? – genpfault
@genpfault,** while循环是缓慢的部分** – Veridian