With std::string
, +=
is usually quite fast as it can just copy bytes into already allocated buffers. Usually, the L"<" + tag + L">";
will require three or more memory allocations, which are completely unnecessary, if you simply replace that line of code with three +=
. Additionally, allocations are REALLY REALLY SLOW if you have Visual Studio start the program for you, even release builds. Run your program manually without Visual Studio, and see if that solves your performance problems.
I dug into the MFC source to very this. (And dug and dug and dug...) and found that ATL::CSimpleStringT::PrepareWrite2(int nLength)
will grow exponentially (1.5x bigger each allocation, completely normal, std::string
is the same, except...
If the MFC string is over 1G, it only adds 1M each allocation after that.
So there's two conditions:
If strContainer
is over 1G, you should manually reserve memory (Preallocate
a large number of bytes. It doesn't have to be exact, or even greater than the real number.).
Otherwise, simply replace the +
with +=
.