I am using an ostringstream to hold text which is written to a file using an ofstream object, but it is unacceptably slow - it takes more than 10 secs to write 200kb on a fast pc. Please can you tell me how to speed this up significantly.
In the document's OnSave function(I'm using MFC) I create an ostringstream and pass it to a function to be filled with data, then I write the text string to the ofstream:
where pData is a pointer to a data object (CData*) held by the document. CData is a base class for CDataReal and CDataComplex, which hold real and complex data respectively. In the derived class I do the following to write each row of a 2d array on separate lines with a comma delimiter:Code:BOOL CMyDocument::OnFileSave(lpszPathName) { ofstream fout; fout.open(lpszPathName); ostringstream os; pData->TextOut(os); fout << os.str(); }
I don't necessarily have to pass an ostringstream object - it just seemed like a neat solution. Could someone please explain why the above is SO slow.Code:void CDataReal::TextOut(ostringstream& os) { for (int i=0; i < someIndex; ++i) { for (int j=0; j < someIndex; ++j) { os << "," << m_TemplateData.begin()[j][i]; } os << endl; } }
Thank you




Reply With Quote