Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency of C-String vs C++Strings

Tags:

c++

string

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?

like image 830
James Leonard Avatar asked Aug 25 '12 17:08

James Leonard


People also ask

Are C strings faster?

C-strings are usually faster, because they do not call malloc/new.

Should I use Cstring or string?

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.

What is the difference between strings and C strings?

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.

What is the difference between C-style strings and C++ style strings?

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.


2 Answers

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.

like image 175
Leonid Volnitsky Avatar answered Sep 17 '22 23:09

Leonid Volnitsky


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!

like image 44
Nawaz Avatar answered Sep 16 '22 23:09

Nawaz