473,606 Members | 2,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6885
roberts.n...@gm ail.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...@gm ail.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**********@gm ail.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**********@gm ail.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**********@gm ail.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**********@gm ail.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_stri ng 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**********@gm ail.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::si ze().

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

std::string toUpper(std::st ring 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**********@gm ail.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

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

Similar topics

7
3561
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
3292
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
1922
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 when compiled with 6.0 it contains string from Vector (that's how I want it to work). std::vector<std::string> Vector; ... void MyClass::DoThis(std::vector<std::string> Vector) { const char *arrString;
33
3659
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 has prepared for those who love him" 1 Cor 2:9
2
10882
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 think for a sec. At first I read it as converting a 'const char *' to a 'std::string *' void f(const std::string &s) { std::cout << s.size() << "\n";
3
9519
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++ code (ie. wrappers etc). In Managed C++ there was an automatic conversion between const char* and String^. This was useful to us for two reasons: 1. We declared our string constants as eg. const char* const c_My_Constant = "blah", and these...
5
1975
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 std::string. What is the prefered way?
5
4548
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
3730
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 like to remove the weekday information that is "Wed" here, and I also would like to replace the spaces char by "_" and also remove the "\n" char. I didn't know how to truncate the string from beginning or replace some chars in a string with another...
0
8031
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7962
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8443
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8107
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6792
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5467
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2452
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1309
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.