Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do std::string operations perform poorly?
    primarykey
    data
    text
    <p>I made a test to compare string operations in several languages for choosing a language for the server-side application. The results seemed normal until I finally tried C++, which surprised me a lot. So I wonder if I had missed any optimization and come here for help.</p> <p>The test are mainly intensive string operations, including concatenate and searching. The test is performed on Ubuntu 11.10 amd64, with GCC's version 4.6.1. The machine is Dell Optiplex 960, with 4G RAM, and Quad-core CPU.</p> <h2>in Python (2.7.2):</h2> <pre><code>def test(): x = "" limit = 102 * 1024 while len(x) &lt; limit: x += "X" if x.find("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0) &gt; 0: print("Oh my god, this is impossible!") print("x's length is : %d" % len(x)) test() </code></pre> <p>which gives result:</p> <pre><code>x's length is : 104448 real 0m8.799s user 0m8.769s sys 0m0.008s </code></pre> <h2>in Java (OpenJDK-7):</h2> <pre><code>public class test { public static void main(String[] args) { int x = 0; int limit = 102 * 1024; String s=""; for (; s.length() &lt; limit;) { s += "X"; if (s.indexOf("ABCDEFGHIJKLMNOPQRSTUVWXYZ") &gt; 0) System.out.printf("Find!\n"); } System.out.printf("x's length = %d\n", s.length()); } } </code></pre> <p>which gives result:</p> <pre><code>x's length = 104448 real 0m50.436s user 0m50.431s sys 0m0.488s </code></pre> <h2>in Javascript (Nodejs 0.6.3)</h2> <pre><code>function test() { var x = ""; var limit = 102 * 1024; while (x.length &lt; limit) { x += "X"; if (x.indexOf("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0) &gt; 0) console.log("OK"); } console.log("x's length = " + x.length); }(); </code></pre> <p>which gives result:</p> <pre><code>x's length = 104448 real 0m3.115s user 0m3.084s sys 0m0.048s </code></pre> <h2>in C++ (g++ -Ofast)</h2> <p>It's not surprising that Nodejs performas better than Python or Java. But I expected libstdc++ would give much better performance than Nodejs, whose result really suprised me.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; using namespace std; void test() { int x = 0; int limit = 102 * 1024; string s(""); for (; s.size() &lt; limit;) { s += "X"; if (s.find("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0) != string::npos) cout &lt;&lt; "Find!" &lt;&lt; endl; } cout &lt;&lt; "x's length = " &lt;&lt; s.size() &lt;&lt; endl; } int main() { test(); } </code></pre> <p>which gives result:</p> <pre><code>x length = 104448 real 0m5.905s user 0m5.900s sys 0m0.000s </code></pre> <h2>Brief Summary</h2> <p>OK, now let's see the summary:</p> <ul> <li>javascript on Nodejs(V8): 3.1s</li> <li>Python on CPython 2.7.2 : 8.8s</li> <li>C++ with libstdc++: 5.9s</li> <li>Java on OpenJDK 7: 50.4s</li> </ul> <p>Surprisingly! I tried "-O2, -O3" in C++ but noting helped. C++ seems about only 50% performance of javascript in V8, and even poor than CPython. Could anyone explain to me if I had missed some optimization in GCC or is this just the case? Thank you a lot.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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