I have a large amount of big matrices with values (integers) ranging from -1 to 15, which I want to write to a text file with the function below. The writing speed seems to be about 0.1 MB/s, so I have played around a bit to see if I can make it faster without any results. How do I make it faster?
bool mymap::write_one_mat(string txtfile, matrix& mat)
{
ofstream myfile (txtfile, ios::app|ios::binary);
int element;
if (myfile.is_open())
{
int rows = mat.get_rows();
int cols = mat.get_cols();
myfile << "<";
for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= cols; ++j)
{
element = mat.get_element(i,j);
if(element < 0 || element > 9)
{
myfile << to_string(element);
}
else
{
myfile << " ";
myfile << to_string(element);
}
}
}
myfile << ">\n";
myfile.close();
return true;
}
else
return false;
}
Taking from the comments you have:
With the addition that
txtfile
's andmat
's type has been changed to a reference toconst
. This makes sense since yourwrite_one_mat
method doesn't modify its parameters. Make sure thatmat::get_rows()
,mat::get_cols()
andget_element()
areconst
methods so they can be called onmat
.