CString + = statement performance issues

I am working on an old MFC / C ++ project that parses large text files using the MFC CString class to process strings. I noticed that during the parsing process, there are many additions of small details to the overall large CString object as such:

//'strContainer' = CString
//'tag' = CString of a much smaller size
strContainer += L"<" + tag + L">";

The above statement slows down the overall performance of CString when a variable strContainerreaches a certain larger size. I assume this is due to the frequent reallocation of memory performed by the statement +=.

So, I was curious if there is a way to improve this?

PS1 I do not know the size of the result line in front to pre-select it.

PS2. I have to stick with CString because of the complexity of the project itself. (Or I can't switch to Boost or other newer implementations.)

+4
source share
1 answer

std::string, += , . L"<" + tag + L">"; , , +=. , , Visual Studio , . Visual Studio , .

MFC . ( ...) , ATL::CSimpleStringT::PrepareWrite2(int nLength) ( 1,5 , , std::string , ...
MFC 1G, 1M.

, : strContainer 1G, (Preallocate . , .). + +=.

+5

Source: https://habr.com/ru/post/1532577/


All Articles