I have a project for school where I have a *.txt file with ~2M lines (~42MB) and each line contains row number, column number and value. I am parsing these into three vectors (int, int, float) but it takes around 45sec to complete. And I am looking for some way to make it faster. I guess the bottleneck is the iteration through every element and it would be better to load one chunk of rows/columns/values and put them into a vector at once. Unfortunately, I do not know how to do that, or if its even possible. Also I would like to stick to STL. Is there a way I could make it faster?
Thanks!
file example (first line has the count of rows, columns and non-zero values):
1092689 2331 2049148
1 654 0.272145
1 705 0.019104
2 245 0.812118
2 659 0.598012
2 1043 0.852509
2 1147 0.213949
For now I am working with:
void LoadFile(const char *NameOfFile, vector<int> &row,
vector<int> &col, vector<float> &value) {
unsigned int columns, rows, countOfValues;
int rN, cN;
float val;
ifstream testData(NameOfFile);
testData >> rows >> columns >> countOfValues;
row.reserve(countOfValues);
col.reserve(countOfValues);
value.reserve(countOfValues);
while (testData >> rN >> cN >> val) {
row.push_back(rN);
col.push_back(cN);
value.push_back(val);
}
testData.close();
}