473,387 Members | 1,834 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,387 software developers and data experts.

benchmarks of char* ops vs. std::string

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.

Dec 6 '05 #1
15 6865
roberts.n...@gmail.com 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

Dec 6 '05 #2

mlimber wrote:
roberts.n...@gmail.com 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.

Dec 6 '05 #3

ro**********@gmail.com 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

Dec 6 '05 #4
ro**********@gmail.com 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
Dec 6 '05 #5

ro**********@gmail.com 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

Dec 6 '05 #6
On 6 Dec 2005 09:40:50 -0800, ro**********@gmail.com 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
Dec 6 '05 #7

Roland Pibinger wrote:
Long-String-Pessimization


Google is not helping me on this one.

Dec 6 '05 #8
ro**********@gmail.com 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
Dec 6 '05 #9
ro**********@gmail.com schrieb:
Roland Pibinger wrote:

Long-String-Pessimization

Google is not helping me on this one.


You snipped:
(Small-String-Optimization, Long-String-Pessimization)


LSP is the literal opposite to SSO.

Thomas
Dec 6 '05 #10
On 6 Dec 2005 13:52:55 -0800, ro**********@gmail.com 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
Dec 6 '05 #11
Thomas J. Gritzan wrote:
(Small-String-Optimization, Long-String-Pessimization)


LSP is the literal opposite to SSO.


Huh... I can see how "Long" is opposite to "Short" and how
"Pessimization" is opposite to "Optimization", but to be literal,
shouldn't SSO result into something like Long-Point-Pessimization
(or whatever is the literal opposite of "String")? :-)
Dec 7 '05 #12
Victor Bazarov schrieb:
Thomas J. Gritzan wrote:
(Small-String-Optimization, Long-String-Pessimization)

LSP is the literal opposite to SSO.

Huh... I can see how "Long" is opposite to "Short" and how
"Pessimization" is opposite to "Optimization", but to be literal,
shouldn't SSO result into something like Long-Point-Pessimization


....
(or whatever is the literal opposite of "String")? :-)


Maybe "array", so: Long-Array-Pessimization. :-)

Thomas
Dec 7 '05 #13
You do not need any kind of benchmark here. You are working in C++?
Do it the C++ way; you are arguing in favor of the default. If your
char*-fanatic wants to do it old way, the onus is on him to prove it is
better, not you to prove it is worse.

The std::string class is probably among the most fundamentally obvious
and beneficial improvement the STL offers over C. Preferring char*
isn't "reinventing the wheel", it is ignoring the wheel entirely in
favor of running everywhere. Uphill.

Dec 7 '05 #14
ro**********@gmail.com 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.


Depends a lot on the std::string implementation, but you probably want
to google for Herb Sutter's articles on this. Personally, I've seen an
order
of magnitude (better for std::string). Still, my own string class did
even
better in that case. But the real win was that the change from
std::string
to my::string was a one-minute fix. Going from char* to std::string
took
a complete rewrite. That's the real profit of std::string, if you don't
like
the implementation it's a lot easier to replace. Replacing char* in an
existing program is a bit tricky. It probably involves changing the
compiler ;)

HTH,
Michiel Salters

Dec 7 '05 #15
Roland Pibinger wrote:
(Small-String-Optimization, Long-String-Pessimization)


Wrong. SSO can also be a LSO.

One reason is that SSO avoids trips to the memory allocator for small
strings. This reduces the load on the memory allocator. I'm not aware
of
any allocator which performs worse without these many small
allocations.
However, there are many that do better if you drop all those small
allocations.
For instance, some allocators have issues with fragmentation. The
deallocation of small strings leaves holes, that have to be considered
when
allocating large strings. When these small holes don't exists due to
SSO,
the large string allocation is faster. Multithreaded environments are
another:
less contention means faster allocators.

HTH,
Michiel Salters

Dec 7 '05 #16

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

Similar topics

7
by: Sims | last post by:
Hi, if i have a code const char * GetValue() { std::string szVectorValue = ...// get a std::string from the vector return szVectorValue.c_str(); }
9
by: Jim Langston | last post by:
#include <string> int main () { std::string MyString = "Testing"; MyString = " " + MyString; } This works in Microsoft Visual C++ .net 2003
2
by: anelma via .NET 247 | last post by:
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself? arrString content will be garbage with .net compilation, but...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
5
by: Olaf | last post by:
Hi, I wrap a legacy C library, e.g. the signature is void set_error_buffer(char* buf); where the buf length should be of length of 512 (it's defined). Now I want to wrap it with...
5
by: Ramesh | last post by:
Hi. Assuming I have a code snippet like below: #include <iostream> using namespace std; char Mac = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 }; std::string csMac;
13
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.