Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Performance of CString::Replace

343 views
Skip to first unread message

dududuil

unread,
Apr 23, 2010, 4:07:01 AM4/23/10
to
hi all,
I have a huge application that does a lot of strings manipulations.
Analyzing its performance, I have noticed that a lot of time is spend in
CString::Replace.

calls like :
str.Replace("\n","\n\t"); or
str.Replace("\r", "\0x00");
take lots of time. (25% of overall performance !!)

How can I improve the performance?


David Lowndes

unread,
Apr 23, 2010, 5:22:08 AM4/23/10
to
>I have a huge application that does a lot of strings manipulations.
>Analyzing its performance, I have noticed that a lot of time is spend in
>CString::Replace.
>
>calls like :
>str.Replace("\n","\n\t"); or
>str.Replace("\r", "\0x00");
>take lots of time. (25% of overall performance !!)

If you change the second example to:

str.Replace( '\r', '\0x00');

It might result in some improvement.

However the first example needs to insert an extra character, so
there's no easy way to alter the fact that the string will require
reallocation - which is likely to be the main hog.

Dave

Ulrich Eckhardt

unread,
Apr 23, 2010, 6:27:22 AM4/23/10
to

The call to Replace() has to allocate a second buffer, transform the content
accordingly into the new buffer and then release the old one. It's the
allocating and releasing that probably takes the performance, or at least
it's the part that can be avoided mostly.

If you actually have more than one call to Replace() on the same string,
consider rolling your own function that does the replacements. If you have
the two above, I'd do it like this:

CString tmp;
tmp.Reserve(str.GetLength()); // preallocate, not sure if MFC allow this
for(int i=0, len=str.GetLength(); i!=len; ++i)
{
switch(str[i]) {
case '\n': tmp += "\n\t"; break;
case '\r': tmp += "\0x00"; break;
default: tmp += str[i]; break;
}
}
str = tmp;

The preallocation makes the target string overallocate its internal buffer,
so that not every call that adds something causes an allocation.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

dududuil

unread,
Apr 26, 2010, 7:51:01 AM4/26/10
to
I have failed to find how to preallocate a CString to a certin size - does
anyone know how can I set the size of a CString ?


"Ulrich Eckhardt" wrote:

> .
>

Doug Harrison [MVP]

unread,
Apr 26, 2010, 1:50:39 PM4/26/10
to
On Mon, 26 Apr 2010 04:51:01 -0700, dududuil
<dudu...@discussions.microsoft.com> wrote:

>I have failed to find how to preallocate a CString to a certin size - does
>anyone know how can I set the size of a CString ?

CSimpleStringT::Preallocate. Microsoft made the CString documentation
pretty inscrutable a couple of versions ago, and you have to look at
CStringT and its base class CSimpleStringT at a minimum.

--
Doug Harrison
Visual C++ MVP

Tom Serface

unread,
Apr 26, 2010, 8:51:46 PM4/26/10
to
You can also create a CString using the constructor:

CString(TCHARch,intnRepeat=1);

Where the repeat character is the number of characters you want ultimately.

You might also want to try a regex engine like boost to see if that is
faster:

http://onlamp.com/pub/a/onlamp/2006/04/06/boostregex.html

Tom

"dududuil" <dudu...@discussions.microsoft.com> wrote in message
news:69E810BD-F736-4B2E...@microsoft.com...

0 new messages