I'm trying to read from a file in a faster way. The current way I'm doing it is the following, but it is very slow for large files. I am wondering if there is a faster way to do this? I need the values stored a struct, which I have defined below.
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++;
}
my struct:
struct matEntry {
int row;
int col;
float val;
};
The file is formatted like this (int, int, float):
1 2 7.9
4 5 9.008
6 3 7.89
10 4 10.21
More info:
- I know the number of lines in the file at run time.
- I am facing a bottleneck. The profiler says the while() loop is the bottleneck.