[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Speed question regarding 'string' type

Posted on 2005-04-08
4
Medium Priority
?
227 Views
Last Modified: 2013-11-20
Hi,

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.
0
Comment
Question by:StanChart
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13740850
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 :-)
Cheers!
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 1200 total points
ID: 13743225
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
}

str.Append(newString);
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 800 total points
ID: 13743481
FYI:
If you're looking for speed, in general, CString peformance faster then std::string that comes with VC++.

0
 

Author Comment

by:StanChart
ID: 13765098
You guys are really good! It's A LOT faster!!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

873 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