By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,257 Members | 2,812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,257 IT Pros & Developers. It's quick & easy.

Is there systematic performance comparison of std::string and c style string?

P: n/a
Is there any comparison data on perfomance difference between
std::string and c style string? Or maybe if there are source code
which could be used to measuer on different compiler/platform, in a
systematic way?

Aug 11 '07 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On 2007-08-11 16:10, yu****@sina.com wrote:
Is there any comparison data on perfomance difference between
std::string and c style string? Or maybe if there are source code
which could be used to measuer on different compiler/platform, in a
systematic way?
Most certainly there is, google is your friend. I believe if you include
the word rope in the search you'll find some. Remember though that all
the benchmarks in the world does not mean shit if they don't measure
what you need, so instead of looking for other peoples benchmarks
perform your own with the code you need to run.

--
Erik Wikström
Aug 11 '07 #2

P: n/a
On Aug 11, 10:19 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-08-11 16:10, yu_...@sina.com wrote:
Is there any comparison data on perfomance difference between
std::string and c style string? Or maybe if there are source code
which could be used to measuer on different compiler/platform, in a
systematic way?

Most certainly there is, google is your friend. I believe if you include
the word rope in the search you'll find some. Remember though that all
the benchmarks in the world does not mean shit if they don't measure
what you need, so instead of looking for other peoples benchmarks
perform your own with the code you need to run.

--
Erik Wikström
Thanks for your reply and suggestion. Actually I myself am prety
convinced to prefer std::string, but it's simply not a common sense of
my colleagues. That's why I'm searching for hard proofs to convince
people. I did googled for some time, but didn't find what I can
directly use. Any way I could write some code to compare functionality
we are interested, just as you have suggested.

Regards,
Kevin

Aug 12 '07 #3

P: n/a
On 2007-08-12 11:35, yu****@sina.com wrote:
On Aug 11, 10:19 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2007-08-11 16:10, yu_...@sina.com wrote:
Is there any comparison data on perfomance difference between
std::string and c style string? Or maybe if there are source code
which could be used to measuer on different compiler/platform, in a
systematic way?

Most certainly there is, google is your friend. I believe if you include
the word rope in the search you'll find some. Remember though that all
the benchmarks in the world does not mean shit if they don't measure
what you need, so instead of looking for other peoples benchmarks
perform your own with the code you need to run.

--
Erik Wikström

Thanks for your reply and suggestion. Actually I myself am prety
convinced to prefer std::string, but it's simply not a common sense of
my colleagues. That's why I'm searching for hard proofs to convince
people. I did googled for some time, but didn't find what I can
directly use. Any way I could write some code to compare functionality
we are interested, just as you have suggested.
I might have miss understood you original question a bit, I thought that
you wanted to know which was the best performer for a certain kind of
use (such as really large amounts of strings or really large strings) in
which case there might sometimes be some benefits to using C-strings.

But if you mean usage of std::string vs. C-strings in general then I'm
very hard pressed to come up with any argument in favour of C-strings
but it's quite easy to find arguments for std::string (ease of use, no
risk of overflows, don't have to allocate memory manually etc.). In fact
one very good argument against using C-strings is that most buffer
overflow attacks are caused by improper usage of C-strings, and if
std::string had been used instead the code would have been much simpler
and more safe. As for speed, I'd say that std::string is fast enough for
most usages and if your application is an exception you'd probably know
it due to profiling and benchmarks you've already done.

--
Erik Wikström
Aug 12 '07 #4

P: n/a
<yu****@sina.comwrote in message
news:11**********************@g12g2000prg.googlegr oups.com...
Is there any comparison data on perfomance difference between
std::string and c style string? Or maybe if there are source code
which could be used to measuer on different compiler/platform, in a
systematic way?
In my own testing the overhead of std::string .vs. c-style strings was
measured in microseconds. I.E. very negligable.
Aug 13 '07 #5

P: n/a
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:H_*************@newsfe04.lga...
<yu****@sina.comwrote in message
news:11**********************@g12g2000prg.googlegr oups.com...
>Is there any comparison data on perfomance difference between
std::string and c style string? Or maybe if there are source code
which could be used to measuer on different compiler/platform, in a
systematic way?

In my own testing the overhead of std::string .vs. c-style strings was
measured in microseconds. I.E. very negligable.
Wait,not micro, the one that is smaller than nano. Lets see, mili, micro,
nano, ... umm.. dang.
Aug 13 '07 #6

P: n/a
Jim Langston wrote:
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:H_*************@newsfe04.lga...
>In my own testing the overhead of std::string .vs. c-style strings
was measured in microseconds. I.E. very negligable.

Wait,not micro, the one that is smaller than nano. Lets see, mili,
micro, nano, ... umm.. dang.
Pico? Atto? Femto?
Aug 13 '07 #7

P: n/a
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:8J******************************@comcast.com. ..
Jim Langston wrote:
>"Jim Langston" <ta*******@rocketmail.comwrote in message
news:H_*************@newsfe04.lga...
>>In my own testing the overhead of std::string .vs. c-style strings
was measured in microseconds. I.E. very negligable.

Wait,not micro, the one that is smaller than nano. Lets see, mili,
micro, nano, ... umm.. dang.

Pico? Atto? Femto?
Pico, that's it. It took about 4 pico seconds longer to allocate a
std::string than to use a c-style array in my testing. Negligable for any
application.
Aug 13 '07 #8

P: n/a
On Aug 13, 5:03 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message
news:8J******************************@comcast.com. ..
Jim Langston wrote:
"Jim Langston" <tazmas...@rocketmail.comwrote in message
news:H_*************@newsfe04.lga...
In my own testing the overhead of std::string .vs. c-style strings
was measured in microseconds. I.E. very negligable.
Wait,not micro, the one that is smaller than nano. Lets see, mili,
micro, nano, ... umm.. dang.
Pico? Atto? Femto?
Pico, that's it. It took about 4 pico seconds longer to
allocate a std::string than to use a c-style array in my
testing. Negligable for any application.
Picosecond differences are probably less than the resolution of
your measurement system; it would be more accurate to say that
you found no measurable difference. But that still doesn't tell
us anything, because we don't know what you were measuring.

Note too that for any given activity, the implementation of
std::string can make a significant difference. For some things,
the implementation in g++ is significantly faster than that in
VC++, for others, the reverse is true. (G++ uses reference
counting; VC++ deep copy with the small string optimization. If
you don't copy much, and most of your strings are short, VC++
will be faster; if you copy long strings a lot, g++.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 13 '07 #9

P: n/a
On Aug 12, 6:58 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-08-12 11:35, yu_...@sina.com wrote:


On Aug 11, 10:19 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-08-11 16:10, yu_...@sina.com wrote:
Is there any comparison data on perfomance difference between
std::string and c style string? Or maybe if there are source code
which could be used to measuer on different compiler/platform, in a
systematic way?
Most certainly there is, google is your friend. I believe if you include
the word rope in the search you'll find some. Remember though that all
the benchmarks in the world does not mean shit if they don't measure
what you need, so instead of looking for other peoples benchmarks
perform your own with the code you need to run.
--
Erik Wikström
Thanks for your reply and suggestion. Actually I myself am prety
convinced to prefer std::string, but it's simply not a common sense of
my colleagues. That's why I'm searching for hard proofs to convince
people. I did googled for some time, but didn't find what I can
directly use. Any way I could write some code to compare functionality
we are interested, just as you have suggested.

I might have miss understood you original question a bit, I thought that
you wanted to know which was the best performer for a certain kind of
use (such as really large amounts of strings or really large strings) in
which case there might sometimes be some benefits to using C-strings.

But if you mean usage of std::string vs. C-strings in general then I'm
very hard pressed to come up with any argument in favour of C-strings
but it's quite easy to find arguments for std::string (ease of use, no
risk of overflows, don't have to allocate memory manually etc.). In fact
one very good argument against using C-strings is that most buffer
overflow attacks are caused by improper usage of C-strings, and if
std::string had been used instead the code would have been much simpler
and more safe. As for speed, I'd say that std::string is fast enough for
most usages and if your application is an exception you'd probably know
it due to profiling and benchmarks you've already done.

--
Erik Wikström- Hide quoted text -

- Show quoted text -
Actually we are dealing with telecomunication protocols, like SIP and
Diameter. Most contents are now text based, so we are right dealing
with large amount of strings, and sometimes the string can be large
(To about Mega bytes). Most operation on string would be copy, find
and concatecation, very little modify or replace operation.

And std::string haven't been widely used yet, so I can't just change
it overnight. That's why I have to do some work outside our
application, and the analysis should better cover as much operations
on strings of different length. That's not a very simple work to do,
I'm just lazy and wonder if somebody had done that.

Regards,
Kevin

Aug 13 '07 #10

P: n/a
On Aug 13, 9:20 am, James Kanze <james.ka...@gmail.comwrote:
Note too that for any given activity, the implementation of
std::string can make a significant difference. For some things,
the implementation in g++ is significantly faster than that in
VC++, for others, the reverse is true. (G++ uses reference
counting; VC++ deep copy with the small string optimization. If
you don't copy much, and most of your strings are short, VC++
will be faster; if you copy long strings a lot, g++.)
I've also noticed a lot of code out there where people
do this:
void foo(std::string s) { ... }

where they could be doing this instead:
void foo(const std::string& s) { ... }

Presumably this is a Java / C# influence, but appied
systematically across a codebase with a "copying
std::string" library, this is going to involve a lot
of extra copying for zero benefit, which I suspect
cannot be optimised away in the general case. I
wonder how many "performance problems" reported
with std::string could be eliminated by correcting
this usage.

I think it's "More Exceptional C++" by Herb Sutter
that has an appendix comparing different types of
string implementation strategy, including problems
with some implementations of reference counting.

Aug 13 '07 #11

P: n/a
Hi!

yu****@sina.com schrieb:
Actually we are dealing with telecomunication protocols, like SIP and
Diameter. Most contents are now text based, so we are right dealing
with large amount of strings, and sometimes the string can be large
(To about Mega bytes). Most operation on string would be copy, find
and concatecation, very little modify or replace operation.
Try using std::ostringstream for concatenation of multiple strings,
because the std::string::operator + will not allocate more memory than
is necessary to hold just the two operands. That is, compare:
s1 + s2 + s3 + " WHERE " + s4 + s5
with:
stream << s1 << s2 << s3 << " WHERE " << s4 << s5;
which may be faster. (AFAIK, Java is doing such a conversion from
operator + to streams (StringBuffer in Java) automatically when compiling.)

Frank
Aug 13 '07 #12

P: n/a
Hi!

Glyn Davies schrieb:
The reserve issue I mentioned above came in where there was a lot of
string concatenation going on. A quick check of lengths + a reserve
stopped a lot of churn, and improved performance no end.
I'm using std::ostringstream in case of concatenation. It usually
performs better than std::string::operator + when you can't do a resize
because the length is unknown. And you can also to formatting. I'm
actually using this to construct SQL statements with filled in values
like ints.

Frank
Aug 13 '07 #13

P: n/a
On Mon, 13 Aug 2007 02:05:27 -0700, tragomaskhalos <da*************@logicacmg.comwrote:
I've also noticed a lot of code out there where people
do this:
void foo(std::string s) { ... }

where they could be doing this instead:
void foo(const std::string& s) { ... }

Presumably this is a Java / C# influence
Oh yes. I have battled one Java developer who did this on a regular
basis (in C++).

It's ironic, since those languages are all about passing references ...
but appied
systematically across a codebase with a "copying
std::string" library, this is going to involve a lot
of extra copying for zero benefit, which I suspect
cannot be optimised away in the general case. I
wonder how many "performance problems" reported
with std::string could be eliminated by correcting
this usage.
If they do it to std::string, they probably do it to all kinds of
objects, and strings become their smallest problem ...

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Aug 14 '07 #14

P: n/a
"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
On Aug 13, 5:03 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message
news:8J******************************@comcast.com. ..
Jim Langston wrote:
"Jim Langston" <tazmas...@rocketmail.comwrote in message
news:H_*************@newsfe04.lga...
In my own testing the overhead of std::string .vs. c-style strings
was measured in microseconds. I.E. very negligable.
Wait,not micro, the one that is smaller than nano. Lets see, mili,
micro, nano, ... umm.. dang.
Pico? Atto? Femto?
Pico, that's it. It took about 4 pico seconds longer to
allocate a std::string than to use a c-style array in my
testing. Negligable for any application.
Picosecond differences are probably less than the resolution of
your measurement system; it would be more accurate to say that
you found no measurable difference. But that still doesn't tell
us anything, because we don't know what you were measuring.

Note too that for any given activity, the implementation of
std::string can make a significant difference. For some things,
the implementation in g++ is significantly faster than that in
VC++, for others, the reverse is true. (G++ uses reference
counting; VC++ deep copy with the small string optimization. If
you don't copy much, and most of your strings are short, VC++
will be faster; if you copy long strings a lot, g++.)

=========

Yes, I had to measure doing it a few million times to tell the difference
between a c-style string and std::string. And results can be thrown off
depending on how the compiler decides to optimize the code. But I was able
to get a difference of around 4 pico seconds using std::string than a
c-style string when I averaged the results. And I found it negligable.

I did this to show someone who was insistant on using c-style strings than
std::strings because he thought std::strings were slower. Showing him the
code and results he agreed that std::strings were usable.
Aug 15 '07 #15

P: n/a
Thanks all for above valuable disscussions.

Regards,
Kevin

Aug 18 '07 #16

P: n/a
On Aug 11, 5:10 pm, yu_...@sina.com wrote:
Is there any comparison data on perfomance difference between
std::string and c style string? Or maybe if there are source code
which could be used to measuer on different compiler/platform, in a
systematic way?

Look at C/C++ Program Perfometer
http://sourceforge.net/projects/cpp-perfometer/
http://groups.google.com/group/perfo

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
Aug 18 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.