I have 10 large (34 million cell) 2D gridded vectors storing doubles. When written they are over 200 mb in size. I use an ofstream object to write them to a text file (csv format), one element at a time, using two for loops (one for my rows, one for my columns). They take way to long to write. Is there a faster way to write from these vectors?
Here is my code:
// Resizing of vectors
flowDirGrid.resize(rows, std::vector<double>(cols, NODATA));
// Do some processing
....
// Outputting processed data
ofstream outfile3("FlowDir.dat");
if(!outfile3.good())
return;
for(int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
if(elevation[i][j]!=NODATA)
outfile3 << flowDirGrid[i][j]<<" ";
else
outfile3 << NODATA<<" ";
}
outfile3 << std::endl;
}
outfile3.close();
I am using C++ and Visual Studio 2012.
EDIT:. I have removed all the std::endl instances and replaced with "\n" and it still takes 17 minutes to write each output file. I may move to using the recommended C method.
Would using a ternary instead of an if-else ladder speed it up at all?