Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C vs C++ performance (for handling short strings)

Tags:

c++

Edit

My test result is here. Although someone insists on my test is perfectly wrong, C++ was 110% slower than C ;(


Recently, Bjarne Stroustrup have wrote Five Popular Myths about C++

In his article, he implemented a function in C and C++

C++ Version

string compose(const string& name, const string& domain)
{
  return name+'@'+domain;
}

C Version

char* compose(const char* name, const char* domain)
{
  char* res = malloc(strlen(name)+strlen(domain)+2); // space for strings, '@', and 0
  char* p = strcpy(res,name);
p += strlen(name);
  *p = '@';
  strcpy(p+1,domain);
  return res;
}

Finally he mentioned:

which version is likely to be the most efficient? Yes, the C++ version, because it does not have to count the argument characters and does not use the free store (dynamic memory) for short argument strings.

Is this right? although C++ version is shorter than C version I think operator+() of std::string would be similar to C version.

like image 791
Jason Heo Avatar asked Feb 11 '23 07:02

Jason Heo


1 Answers

In at least some cases, yes, the C++ version will be substantially faster.

In particular, some implementations of std::string include what's generally referred to as the "short string optimization" (aka "SSO"). With this, the std::string object itself includes space for a string up to some specific limit (typically around 20 characters). Strings that fit in that buffer can (and will) avoid allocating space on the heap/free store to store their data.

In theory, you could do roughly the same thing C--but when/if you do, you have to define your own structure to hold your string (much like C++ does) and every piece of code that manipulates those string structures needs to know how they work, and manipulate them the same way. C++ makes it easy to wrap that code up into an operator overload to keep the details hidden.

The bottom line is that C could theoretically keep up, but it would be enough more work to make that happen that in practice programs that need to do this sort of manipulation in C++ are almost always faster than their counterparts written in C. Just about all that varies is how much faster they run--sometimes they're only a little faster, but especially where there's a lot of manipulation of relatively small strings, substantial differences (e.g., 2:1 or more) are pretty common. Differences can also be quite large when you need to manipulate really large strings, where C++ gains a lot by being able to find the size in constant time, where strlen requires linear time. For strings small enough to fit entirely in L1 cache, that doesn't mean much, but if you can read one value from L1 vs. reading an entire string from main memory, the difference can be huge.

like image 108
Jerry Coffin Avatar answered Feb 19 '23 06:02

Jerry Coffin