Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > benchmarks of char* ops vs. std::string

Reply
Thread Tools

benchmarks of char* ops vs. std::string

 
 
roberts.noah@gmail.com
Guest
Posts: n/a
 
      12-06-2005
Are there any decent benchmarks of the difference between using c
strings and std::string out there? Google isn't being friendly about
it. Obviously this would be dependant on different implementations but
I don't care. I would be happy to find ANY comparison at this point if
it was valid and semi scientifically done.

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      12-06-2005
(E-Mail Removed) wrote:
> Are there any decent benchmarks of the difference between using c
> strings and std::string out there? Google isn't being friendly about
> it. Obviously this would be dependant on different implementations but
> I don't care. I would be happy to find ANY comparison at this point if
> it was valid and semi scientifically done.


I don't have an answer for you, but I can tell you that most in the C++
community think the benefits of std::string is well worth the cost
(which, as you say, varies between implementations but which is
pragmatically speaking usually "good enough"). In fact, the FAQ for
this group maintains that arrays are evil. Compare:

http://www.parashift.com/c++-faq-lit....html#faq-17.5

Cheers! --M

 
Reply With Quote
 
 
 
 
roberts.noah@gmail.com
Guest
Posts: n/a
 
      12-06-2005

mlimber wrote:
> (E-Mail Removed) wrote:
> > Are there any decent benchmarks of the difference between using c
> > strings and std::string out there? Google isn't being friendly about
> > it. Obviously this would be dependant on different implementations but
> > I don't care. I would be happy to find ANY comparison at this point if
> > it was valid and semi scientifically done.

>
> I don't have an answer for you, but I can tell you that most in the C++
> community think the benefits of std::string is well worth the cost
> (which, as you say, varies between implementations but which is
> pragmatically speaking usually "good enough"). In fact, the FAQ for
> this group maintains that arrays are evil. Compare:
>
> http://www.parashift.com/c++-faq-lit....html#faq-17.5


_I_ know that, but as before I am trying to convince someone that is a
*really* die hard char* fan. Yes, I spent 8 hours yesturday chasing
down buffer overflows caused by char* but every time I use std::string
I get hastled about performance issues. I've shown a lot that
std::string isn't showing up in the profiles I do but it isn't having
the effect I wolud like. I need a benchmark to be at all convincing.

 
Reply With Quote
 
peter steiner
Guest
Posts: n/a
 
      12-06-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Are there any decent benchmarks of the difference between using c
> strings and std::string out there? Google isn't being friendly about
> it. Obviously this would be dependant on different implementations but
> I don't care. I would be happy to find ANY comparison at this point if
> it was valid and semi scientifically done.


i don't know of any concrete performance benchmarks, which probably
wouldn't make much sense anyways. you can be sure that any decent STL
implementation implements sufficiently optimized string and allocation
operations which would give you a hard time to match in custom
implementations.

the c++ performance report gives hard figures of the (practically
non-existent) performance hit for using non-virtual classes. (see
http://www.open-std.org/jtc1/sc22/wg...2002/n1396.pdf)

if you have very special requirements for you string operations that
differ much from the common use case you might investigate implementing
a custom allocator or even string class, but that should not be
necessary in general.

-- peter

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-06-2005
(E-Mail Removed) wrote:
> [..]
> _I_ know that, but as before I am trying to convince someone that is a
> *really* die hard char* fan. [...]
> I need a benchmark to be at all convincing.


No, you don't. Leave it be. Were you paid for the time it took you to
chase that overflow bug? Document that and move on. When your boss asks
you to justify the expense and to suggest improvements, present your
std::string argument then and to him/her, not to char* fans. Things like
this are not worth our time to perpetuate the argument. For any benchmark
proving your point there will be another disproving it. And if there is
no easily available one, any die-hard char* fan will feel challenged to
create such benchmark. That would only result into them wasting their
time on that, after you've already wasted yours.

V
 
Reply With Quote
 
Matteo
Guest
Posts: n/a
 
      12-06-2005

(E-Mail Removed) wrote:
> ... I've shown a lot that
> std::string isn't showing up in the profiles


That should really be enough for him if he's actually concerned about
performance, rather than some odd macho thing. Perhaps char *'s are
faster in some circumstances - say if your application computes strlen
and does nothing else, but since std::strings are much easier to use
safely (w.r.t. the buffer overflows), and have negligible impact in
your code performance, they are easily justifiable.

I would say that in making decisions about which string to use, that
the burden of proof lies on him. Ask him to justify the increased
programmer time and reduced code security for a 0.01% or less speedup
(or whatever the profiler's minimum resolution is). If this is a
decision that needs to be made for some project, I would hope that the
technical lead would find the above argument compelling.

-matt

 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      12-06-2005
On 6 Dec 2005 09:40:50 -0800, (E-Mail Removed) wrote:
>I am trying to convince someone that is a
>*really* die hard char* fan. Yes, I spent 8 hours yesturday chasing
>down buffer overflows caused by char* but every time I use std::string
>I get hastled about performance issues. I've shown a lot that
>std::string isn't showing up in the profiles I do but it isn't having
>the effect I wolud like. I need a benchmark to be at all convincing.


There is no specification of the std::string performance
characteristics ('std::string' means the std::basic_string template).
Thus, e.g. the current Microsoft/Dinkumware SSO/LSP implementation
(Small-String-Optimization, Long-String-Pessimization) has entirely
different performance characteristics than a reference-counted COW
(copy-on-write) implementation. (Sorry for the TLAs.)
Performance aware C++ programmers must adapt their programming style
to their std::string implementation.

Best wishes,
Roland Pibinger
 
Reply With Quote
 
roberts.noah@gmail.com
Guest
Posts: n/a
 
      12-06-2005

Roland Pibinger wrote:

> Long-String-Pessimization


Google is not helping me on this one.

 
Reply With Quote
 
Calum Grant
Guest
Posts: n/a
 
      12-06-2005
(E-Mail Removed) wrote:
> Are there any decent benchmarks of the difference between using c
> strings and std::string out there? Google isn't being friendly about
> it. Obviously this would be dependant on different implementations but
> I don't care. I would be happy to find ANY comparison at this point if
> it was valid and semi scientifically done.
>


The time you're really going to notice the difference is when you
allocate a string, e.g.

char str1[1000]; // Why not 1002?
vs
std::string str1;

For the rest e.g. iterating a string or passing a string around by
reference, there will be no difference. Functions like strlen() will be
slower than std::string::size().

You should be aware of std::string::reserve() if you want to improve the
performance of strings. Avoid passing strings by value. e.g.

std::string toUpper(std::string in);

is bad because it has 2 unnecessary copies. Prefer

void toUpper(const std::string &in, std::string &out);

Finally, what is your application? Generally, any GUI, database or
network-bound application isn't going to benefit one iota from premature
optimization. This is why huge websites work fine in Perl.

Algorithms and architectures are far more important in terms of
performance - no application is perfect but concentrating your efforts
where it will make the least impact is a waste of everyone's time. As a
programmer I want to think more about the problem, and less about the
language.

Calum
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      12-06-2005
On 6 Dec 2005 13:52:55 -0800, (E-Mail Removed) wrote:
>> Long-String-Pessimization

>
>Google is not helping me on this one.


It's just the opposite of SSO
In VC++ > 7.0 "long" strings are copied as 'deep copy' (new memory is
dynamically allocated, the string contents is copied). A short string
contains <= 15 char or <= 7(!) wchar_t. For "long" strings, of course,
a deep copy is much slower than the assignment of a pointer and the
increment of a counter in a COW implementation. Again, using
std::string in a performance-critical implementation means programming
against an implementation, not an interface. e.g.

instead of a function like

std::string getCurrentDirectory(); // ok for COW, not for SSO/LSP

you should use

std::string& getCurrentDirectory (std::string& out);
// current dir is copied into 'out' and returned by reference

Best wishes,
Roland Pibinger
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HotKey Keyboard - Invalid Pointer Ops Daniel Computer Support 1 12-05-2004 09:25 PM
Simple questions on shift ops and promotions Glen Able C++ 4 01-28-2004 12:51 PM
Photo-Ops in Texas ??? Annika1980 Digital Photography 19 10-13-2003 01:02 AM
Re: Sort of OT: I have some photo ops tomorrow MJ@MJ.com Digital Photography 9 09-10-2003 11:48 PM
Re: Struts: How to use JNDI do do a few database ops? Wendy S Java 3 07-13-2003 02:32 AM



Advertisments