Re: Add ... to long strings
On Wed, 5 Nov 2008 21:09:37 -0700, "L.Allan" <lynn.d.allan@gmail.com>
wrote:
"Tom Serface" <tom.nospam@camaswood.com> wrote in message
news:0D9C5B00-13CD-476A-99CE-DF55A7695D30@microsoft.com...
Wow, from a percentage point of view that is quite a difference, although
unless you had to do it often you likely notice the difference, but it is
significant and I can see where in your case it might show up as a more
noticeable difference.
I was surprised at how much of a difference there was. Actually VERY
SURPRISED.
CString += seems to work like strcat, where it first has to find the end of
the buffer. strcat is VERY INEFFICIENT for a lot of appends to a long
buffer.
That's why we old C hackers wrote strcpyEP:
char *
strcpyEP(char *dest, const char *src)
{
while (*dest++ = *src++)
;
return dest-1;
} /* strcpyEP */
This function returns a pointer to the terminating nul, great for
concatenating.
CString should be better, since it knows how long the actual string
is. There does seem to be a lot of unexpected overhead.
As Joe mentioned, the CString issue is different. It uses a naive linear
reallocation strategy, which is essentially O(N^2). This is pretty rampant
in MFC, but with care, it can be ameliorated. For example, I improved MFC's
CSharedFile by implementing an exponential growth policy in a derived class
HGLOBAL_FILE; the salient functions went like this:
void
HGLOBAL_File::GrowFile(DWORD dwNewLen)
{
if (dwNewLen > m_nBufferSize && m_nBufferSize < DWORD(-1)/2)
dwNewLen = max(dwNewLen, m_nBufferSize*2);
base::GrowFile(dwNewLen);
}
The comment in the file header was:
// This class implements an exponential regrowth policy, because CSharedFile is
// linear and has horrible performance even if the regrowth block size is the default
// 4096 bytes. We're talking half a second vs. > 10 sec to write 2 MB.
That was circa 1999, but things like this don't change in nature over time,
just in degree. IIRC, I used this to write shell folder ID lists to a
contiguous buffer for later use with shell clipboard operations.
--
Doug Harrison
Visual C++ MVP