Speed question regarding 'string' type

Posted on 2005-04-08
Last Modified: 2013-11-20

I have a method which receives data 1024 bytes at a time.  The bytes are received in a 'char' buffer and appended to 'string' using the 'string' class' append() method.  If I receive 20MB of data, appending these 1024 byte buffers to the string takes a very long time (at least 5 minutes).  I was wondering if there was a more efficient way of doing this without knowing how much memory to allocate for the string (i.e., the 20 MB size is unknown).  Perhaps I could use strcat and append two 'char's and once the transmission is complete, copy the 'char' buffer into the string?  Should I use something more low level like a bstr?

As always, your help is much appreciated.
Question by:StanChart
    LVL 30

    Expert Comment

    Hi StanChart,
    > >Should I use something more low level like a bstr?

    A low level method wouldn't help you here.
    To speed it up, you need to be able to determined at runtime your approximate max size.
    You can then call CString::GetBuffer(MaxValue), and then call ReleaseBuffer
    That should reserve your reqired buffer size, and limit the amount of times the buffer has to resize and recopy the data.

    David Maisonave :-)
    LVL 48

    Accepted Solution

    Use string::reserve function to allocate large block of memory and string::capacity to test current memory block available for the string. In your case, every call to append requires reallocaltion and copying of the whole string.

    // suppose I want to append 1024 bytes to string str
    if ( str.capacity() < str.length() + 1024 )
        str.reserve(str.length() + 102400);   // will be enough for 100 next append calls

    LVL 30

    Assisted Solution

    If you're looking for speed, in general, CString peformance faster then std::string that comes with VC++.


    Author Comment

    You guys are really good! It's A LOT faster!!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    Title # Comments Views Activity
    Detect CR LF to each line 12 116
    ffmpeg - "rtsp://...... Operation not permitted" 4 58
    Fibonacci challenge 11 61
    changeXy challenge 13 40
    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now