Fastest way to get data from a CSV in C++
Save in the file, how many numbers are written inside. Then, on loading resize the vectors. It could reduce the time a bit.
Of course your second version will be much faster - it merely reads the file into memory, without parsing the values in it. The equivalent of the first version using C-style I/O would be along the lines of
if (FILE *fp = fopen("data.csv", "r")) {
while (fscanf(fp, "%d,%d,%d", &x, &y, &z) == 3) {
xv.push_back(x);
yv.push_back(y);
zv.push_back(z);
}
fclose(fp);
}
which, for me, is about three times faster than the C++-style version. But a C++ version without the intermediate stringstream
while (file >> x >> c >> y >> c >> z) {
xv.push_back(x);
yv.push_back(y);
zv.push_back(z);
}
is almost as fast.