I have a text file with ~4 mio floats, i.e 30MB, and I want to read them into a vector<float>
.
The code I have is very bare bone, and gets the job done
std::fstream is("data.txt", std::ios_base::in);
float number;
while (is >> number)
{
//printf("%f ", number);
number_vec.push_back(number);
}
The problem is that it takes 20-30 s on a modern desktop workstation. At first I assumed I did something stupid, but the more I starred at the code, the more I started accepting that maybe it was just the time it takes to parse all those ascii float values into floats
However, then I remembered that Matlab can read, and parse, the same file almost instantly (disk speed seems to be the limit), so it is obvious that my code is just very inefficient.
The only thing I could think of was to reserve the required elements in the vector in advance, but it didn't improve the situation at all.
Can someone help me understand why? and maybe help writing a faster solution?
EDIT The textfile looks like this:
152.00256 45.8569 5.87214 0.225 -0.0005 .....
i.e. One row, space delimited.