Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not that <code>std::string</code> performs poorly (as much as I dislike C++), it's that string handling is so heavily optimized for those other languages.</p> <p>Your comparisons of string performance are misleading, and presumptuous if they are intended to represent more than just that.</p> <p>I know for a fact that <a href="http://hg.python.org/cpython/file/3ecddf168f1f/Objects/stringobject.c" rel="nofollow noreferrer">Python string objects are completely implemented in C</a>, and indeed on Python 2.7, <a href="http://hg.python.org/cpython/file/3ecddf168f1f/Objects/stringobject.c#l1021" rel="nofollow noreferrer">numerous</a> <a href="http://hg.python.org/cpython/file/3ecddf168f1f/Objects/bytearrayobject.c#l340" rel="nofollow noreferrer">optimizations</a> 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.</p> <p>Javascript has numerous heavily optimized implementations. It's to be expected that string handling is excellent here.</p> <p>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.</p> <p>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 <code>s.reserve(limit);</code> would remove reallocation overhead.</p> <p>I'll repeat that you're only testing a single facet of the languages' <em>implementations</em>. The results for this test do not reflect the overall language speed.</p> <p>I've provided a C version to show how silly such pissing contests can be:</p> <pre><code>#define _GNU_SOURCE #include &lt;string.h&gt; #include &lt;stdio.h&gt; void test() { int limit = 102 * 1024; char s[limit]; size_t size = 0; while (size &lt; 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; } </code></pre> <p>Timing:</p> <pre><code>matt@stanley:~/Desktop$ time ./smash x's length = 104448 real 0m0.681s user 0m0.680s sys 0m0.000s </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload