473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

direct access to the buffer of std::string

Hello,
I have a function that does some decoding (same as urldecode in php)
and it returns std::string
std::string urldecode(const char* in, std::size_t length);
so, this function parses input and constructes output char by char. I
tried to use all sorts of direct access to the buffer of std::string to
be returned but it's always slow. It appered that if I dynamically
allocate memory fill it with output, then create std::string from this
buffer of chars then delete this buffer and return this string is at
least 2 times faster than using std::string with reserved and resized
buffer and then accessing this string trough operator[]. (simple
appending using += <char> slows down thing even 10 times!)

Are there any ways I can make it work in a simple fast way without
recopying memory

Thanks

Jul 23 '05 #1
4 4225
__PPS__ wrote:
Hello,
I have a function that does some decoding (same as urldecode in php)
and it returns std::string
std::string urldecode(const char* in, std::size_t length);
so, this function parses input and constructes output char by char. I
tried to use all sorts of direct access to the buffer of std::string to
be returned but it's always slow. It appered that if I dynamically
allocate memory fill it with output, then create std::string from this
buffer of chars then delete this buffer and return this string is at
least 2 times faster than using std::string with reserved and resized
buffer and then accessing this string trough operator[]. (simple
appending using += <char> slows down thing even 10 times!)


There's no reason for operator[] to be slow. operator+=, on the other
hand, has to check whether there's enough storage allocated.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2
if anyone interested, here's results of my observation:
I used gcc322 and vc71 and in both cases it was (of course) faster to
create dynamic array of chars and put data into it and return it.
The second fastest way is not trough operator[]! In fact, using
std::string::iterator I got ~10% faster execution. Results were the
same if I used c_str() casted to char* and then using this pointer as
though I had a raw array of chars (for g++ iterator version was slower
than the case with c_str()), basicly it's faster to do ++ on a pointer
and access value pointed by it, than every time to calculate offset
from some address (operator[offset]).
All the overhead with std::string came from that before using iterators
or op[] I had to resize string, so that it could contain maxim output
length and it means that newly allocated buffer for string has to be
filled with zeros. Then after function is finished I need to resize
again if the real size is less than the intially allocated. And some
overhead comes from that I use return by value.

Jul 23 '05 #3
__PPS__ wrote:
<snip>
All the overhead with std::string came from that before using iterators
or op[] I had to resize string, so that it could contain maxim output
length and it means that newly allocated buffer for string has to be
filled with zeros. Then after function is finished I need to resize
again if the real size is less than the intially allocated. And some
overhead comes from that I use return by value.


You don't have to fill a std::string with zeros - std::string does
not use char '\0' to denote the end-of-string.
Why do you need to resize to a smaller size before returning
the std::string? The 'length' of the string does not
depend on the size of the raw buffer. It seems that you
are confusing the requirements of 'C' nul-terminated strings
with the C++ std::string.

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #4
You don't understand what I meant.
std::string::iterator (if you read my post) is the fastest way to fill
a string with characters. operator[] is also a good way, but with both
of them (not sure about iterator, but certanly with operator[]) you can
easily go over the allocated buffer (access violation). If, for
example, I know that my string will be some size at the end it's better
to .reserve(newsize) some memory so that latter, as your string grows,
it will not be reallocated multiple times to allow more characters.
Reserve only allocates memory - it doesn't change string's length, and
it's a good way to preallocate storage for the case when you use
append, += or insert methods on the string that change it's size. Since
I use iterator (or operator[]) I have to .resize(newsize) not reserve,
which fills extra allocated chars with zero. Then at the end I need to
resize once again to make my string proper size, to cut off extra
characters. If I used reserve instead of the first resize then the
second resize would overide my string with zeros...

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
8
by: CoolPint | last post by:
Is there any way I can reduce the size of internal buffer to store characters by std::string? After having used a string object to store large strings, the object seems to retain the large...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
6
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
11
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.