I guess it is not a secret std::string is much slower than C string (in most of all aspects: allocation, comparison, search):
// alloc
malloc/memcpy : 1.972 secs
std::string : 2.259 secs
// compare
strcmp : 1.319 secs
std::string.compare : 15.802 secs
// search
strstr : 5.045 secs
memmem : 1.345 secs
std::string.find : 7.774 secs
So my question here would be not how to fix it but why std::string is slow (what STL did make wrong or am I wrong)? This slowness make std::string almost useless. So usage of std::string as a key in associative containers makes the things even worse (since comparisons are required when insert/search).
Just to note the only exception is std::string is copied very fast - because it is ref-counted (thought GCC 5 thinks to remove that).
Attaching my benchmark code to: https://drive.google.com/file/d/0B_jw6pBAvP6bSEtmLTA4RU5zZ00/view?usp=sharing
The code is build using:
g++ -O2 -g perf_cstr_vs_std.cpp -o perf_cstr_vs_std
Build/run environment:
RedHat6 VM + gcc 4.4.7