I am working on an older MFC/C++ project that parses large text files using MFC's CString class to handle strings. I noticed that during the parsing process there's a lot of adding of small parts to an overall large CString object as such:
//'strContainer' = CString
//'tag' = CString of a much smaller size
strContainer += L"<" + tag + L">";
The operator above seems to be slowing the overall performance of CString when strContainer variable reaches a certain larger size. I'm supposing that such happens because of the often re-allocation of memory done by the += operator.
So I was curious, is there any way to improve this?
PS1. I do not know the size of the result string up front to pre-allocate it.
PS2. I have to stick with CString due to complexity of the project itself. (Or, I can't switch to Boost or other newer implementations.)
+=is probably the fast part. The performance issue is probably the+of the temporaries. As such, do+=three times and see if that makes a difference. - Mooing DuckCStringoperators that does memory re-allocation. I conclude it because its performance decreases exponentially with the size ofCStringvariable. - c00000fdPreallocatebefore you start appending, and give it a number that's big enough for 95% of your data. Super easy. (The other 5% it will reallocate like it did before, but usually only once, not 10 times) - Mooing Duck