jortizclaver said:
Hi,
I'm about to develop a new framework for my corporative applications
and my first decision point is what kind of strings to use: std::string
or classical C char*.
Performance in my system is quite importante - it's not a realtime
system, but almost - and I concern about std::string performance in
terms of speed. No doubt to use std implementation is a lot easier but
I can't sacrifice speed.
I'm using Sun Workshop 6. A very basic test shows processing with
std::string can be 3 times slower than using char*. Is there any
improvement in later versions?
You should use generic methods of accessing an object that acts like a
std::string, then you can reimplement as you need. I myself found that
profiling rarely focuses on string operations, if ever.
I did do some performance tests of std::string vs. char[]. Using
std::string poorly, such as creating unnecissary temporaries (don't use
operator +), can greately reduce execution speed if in fact you are in
an area of code that needs speed. stringstream was much faster than
sprintf but strcat was also quite a bit faster than append().
Really its a balancing act. There are many costs of using char[] that
have nothing to do with execution speed including debug time caused by
buffer overruns (code riddled with static sized char arrays can really
cost when it comes time to change features). In the end the cost of
using std::string is negligable (when used reasonably) in execution
speed but of major benefit in development time when compared to char[].
If std::string is costing too much you probably need to look closer at
your algorithm, not the string implementation.
And remember, profile before optimizing. I get into arguments with
coworkers about std::string vs. char[] all the time (their claim is the
cost of allocation which I found to be negligable - big believers in
the Clib part of C++) and std::string is never in the top of a
profile...it is always something else. On the other hand it took me 12
hours to track down a buffer overflow in a char[]...