Why do std::string operations perform poorly? Why do std::string operations perform poorly? python python

Why do std::string operations perform poorly?


It's not that std::string performs poorly (as much as I dislike C++), it's that string handling is so heavily optimized for those other languages.

Your comparisons of string performance are misleading, and presumptuous if they are intended to represent more than just that.

I know for a fact that Python string objects are completely implemented in C, and indeed on Python 2.7, numerous optimizations exist due to the lack of separation between unicode strings and bytes. If you ran this test on Python 3.x you will find it considerably slower.

Javascript has numerous heavily optimized implementations. It's to be expected that string handling is excellent here.

Your Java result may be due to improper string handling, or some other poor case. I expect that a Java expert could step in and fix this test with a few changes.

As for your C++ example, I'd expect performance to slightly exceed the Python version. It does the same operations, with less interpreter overhead. This is reflected in your results. Preceding the test with s.reserve(limit); would remove reallocation overhead.

I'll repeat that you're only testing a single facet of the languages' implementations. The results for this test do not reflect the overall language speed.

I've provided a C version to show how silly such pissing contests can be:

#define _GNU_SOURCE#include <string.h>#include <stdio.h>void test(){    int limit = 102 * 1024;    char s[limit];    size_t size = 0;    while (size < limit) {        s[size++] = 'X';        if (memmem(s, size, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26)) {            fprintf(stderr, "zomg\n");            return;        }    }    printf("x's length = %zu\n", size);}int main(){    test();    return 0;}

Timing:

matt@stanley:~/Desktop$ time ./smash x's length = 104448real    0m0.681suser    0m0.680ssys     0m0.000s


So I went and played a bit with this on ideone.org.

Here a slightly modified version of your original C++ program, but with the appending in the loop eliminated, so it only measures the call to std::string::find(). Note that I had to cut the number of iterations to ~40%, otherwise ideone.org would kill the process.

#include <iostream>#include <string>int main(){    const std::string::size_type limit = 42 * 1024;    unsigned int found = 0;    //std::string s;    std::string s(limit, 'X');    for (std::string::size_type i = 0; i < limit; ++i) {        //s += 'X';        if (s.find("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0) != std::string::npos)            ++found;    }    if(found > 0)        std::cout << "Found " << found << " times!\n";    std::cout << "x's length = " << s.size() << '\n';    return 0;}

My results at ideone.org are time: 3.37s. (Of course, this is highly questionably, but indulge me for a moment and wait for the other result.)

Now we take this code and swap the commented lines, to test appending, rather than finding. Note that, this time, I had increased the number of iterations tenfold in trying to see any time result at all.

#include <iostream>#include <string>int main(){    const std::string::size_type limit = 1020 * 1024;    unsigned int found = 0;    std::string s;    //std::string s(limit, 'X');    for (std::string::size_type i = 0; i < limit; ++i) {        s += 'X';        //if (s.find("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0) != std::string::npos)        //    ++found;    }    if(found > 0)        std::cout << "Found " << found << " times!\n";    std::cout << "x's length = " << s.size() << '\n';    return 0;}

My results at ideone.org, despite the tenfold increase in iterations, are time: 0s.

My conclusion: This benchmark is, in C++, highly dominated by the searching operation, the appending of the character in the loop has no influence on the result at all. Was that really your intention?


The idiomatic C++ solution would be:

#include <iostream>#include <string>#include <algorithm>int main(){    const int limit = 102 * 1024;    std::string s;    s.reserve(limit);    const std::string pattern("ABCDEFGHIJKLMNOPQRSTUVWXYZ");    for (int i = 0; i < limit; ++i) {        s += 'X';        if (std::search(s.begin(), s.end(), pattern.begin(), pattern.end()) != s.end())            std::cout << "Omg Wtf found!";    }    std::cout << "X's length = " << s.size();    return 0;}

I could speed this up considerably by putting the string on the stack, and using memmem -- but there seems to be no need. Running on my machine, this is over 10x the speed of the python solution already..

[On my laptop]

time ./testX's length = 104448real 0m2.055suser 0m2.049ssys 0m0.001s