A performance comparison of the speed of various ways to compare strings in C++. In this test, all comparisons are of not-equal strings.

Idea from #C++ on QuakeNet, where we always advocate using std::string over various char* functions. I wondered what, if any, the penalty for doing so was.

Sources

Things Tested

  • a hand-written naive comparator loop; used as baseline
  • string == string
  • string == const char*
  • strcmp(const char*, const char*) == 0
  • strcmp(const char*, string.c_str()) == 0
  • strcmp(string.c_str(), string.c_str()) == 0
  • string.compare(string)
  • string.compare(const char*)
  • …and then the whole thing with different offsets.


Continue Reading »