Monthly ArchiveFebruary 2011
C++ 10 Feb 2011 18:24:56
C++ String Compare Performance
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
- Ticks counted via cycle.h (local mirror)
- Source: speed-string-compare.cpp
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.