What is the fastest way to head a word (std :: string) using C ++?
On Debian Linux using g ++ 4.6.3 with the -O3 flag, this function using boost::to_lower
will extract 81,450,625 words in about 24 seconds in a single thread of execution on an AMD Phenom (tm) II X6 1090T (3200 MHz) processor.
void Capitalize( std::string& word ) { boost::to_lower( word ); word[0] = toupper( word[0] ); }
This function with std::transform
does the same in about 10 seconds. I clean the virtual machine between testing, so I don't think this difference is an accident:
sync && echo 3 > /proc/sys/vm/drop_caches
void Capitalize( std::string& word ) { std::transform(word.begin(), word.end(), word.begin(), ::tolower); word[0] = toupper( word[0] ); }
Are there faster ways? I would not want to lose portability for speed, but if there are faster ways to do this that work in std C ++ or std C ++ with boost, I would like to try them.
Thanks.
user1356386
source share