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