C++ Primer says
For most applications, in addition to being safer, it is also more efficient to use library strings rather then C-style strings
Safety is understood. Why is C++ strings library more efficient? After all, underneath it all, aren't strings still represented as character arrays?
To clarify, does author talk about programmer efficiency (understood) or processing efficiency?
C-strings are usually faster, because they do not call malloc/new.
Use string. cstring is so 1970's. string is a modern way to represent strings in c++. you'll need to learn cstring because you will run into code that uses it.
std::string is compatible with STL algorithms and other containers. C strings are not char * or const char * ; they are just null-terminated character arrays. Even string literals are just character arrays.
Neither C or C++ have a default built-in string type. C-strings are simply implemented as a char array which is terminated by a null character (aka 0 ). This last part of the definition is important: all C-strings are char arrays, but not all char arrays are c-strings.
C-strings are usually faster, because they do not call malloc/new. But there are cases where std::string
is faster. Function strlen()
is O(N), but std::string::size()
is O(1).
Also when you search for substring, in C strings you need to check for '\0'
on every cycle, in std::string
- you don't. In a naive substring search algorithm it doesn't matter much, because instead of checking for '\0'
you need to check for i<s.size()
. But modern high-performance substring search algorithms traverse strings in multibyte steps. And the need for a '\0'
check in every byte slows them down. This is the reason why GLIBC memmem
is x2 times faster than strstr
. I did a lot of benchmarking of substring algorithms.
This is true not only for substring search algorithm. Many other string processing algorithms are slower for zero-terminated strings.
Why is C++ strings library more efficient? After all, underneath it all, aren't strings still represented as character arrays?
Because the code which uses char*
or char[]
is more likely to be inefficent if not written carefully. For example, have you seen loop like this:
char *get_data(); char const *s = get_data(); for(size_t i = 0 ; i < strlen(s) ; ++i) //Is it efficent loop? No. { //do something }
Is that efficient? No. The time-complexity of strlen()
is O(N)
, and furthermore, it is computed in each iteration, in the above code.
Now you may say "I can make it efficient if I call strlen()
just once.". Of course, you can. But you have to do all that sort of optimization yourself and conciously. If you missed something, you missed CPU cycles. But with std::string
, many such optimization is done by the class itself. So you can write this:
std::string get_data(); std::string const & s = get_data(); //avoid copy if you don't need it for(size_t i = 0 ; i < s.size() ; ++i) //Is it efficent loop? Yes. { //do something }
Is that efficient? Yes. The time-complexity of size()
is O(1)
. No need to optimize it manually which often makes code look ugly and hard to read. The resulting code with std::string
is almost always neat and clean in comparison to char*
.
Also note that std::string
not only makes your code efficent in terms of CPU cycles, but it also increases programmer efficency!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With