Note that there are some explanatory texts on larger screens.

plurals
  1. POPerformance comparison of immutable string concatenation between Java and Python
    primarykey
    data
    text
    <p>UPDATES: thanks a lot to Gabe and Glenn for the detailed explanation. The test is wrote not for language comparison benchmark, just for my studying on VM optimization technologies. </p> <p>I did a simple test to understand the performance of string concatenation between Java and Python. </p> <p>The test is target for the default immutable String object/type in both languages. So I don't use StringBuilder/StringBuffer in Java test.</p> <p>The test simply adds strings for 100k times. Java consumes ~32 seconds to finish, while Python only uses ~13 seconds for Unicode string and 0.042 seconds for non Unicode string.</p> <p>I'm a bit surprise about the results. I thought Java should be faster than Python. What optimization technology does Python leverage to achieve better performance? Or String object is designed too heavy in Java?</p> <p>OS: Ubuntu 10.04 x64 JDK: Sun 1.6.0_21 Python: 2.6.5</p> <p>Java test did use -Xms1024m to minimize GC activities.</p> <p>Java code:</p> <pre><code>public class StringConcateTest { public static void test(int n) { long start = System.currentTimeMillis(); String a = ""; for (int i = 0; i &lt; n; i++) { a = a.concat(String.valueOf(i)); } long end = System.currentTimeMillis(); System.out.println(a.length() + ", time:" + (end - start)); } public static void main(String[] args) { for (int i = 0; i &lt; 10; i++) { test(1000 * 100); } } </code></pre> <p>}</p> <p>Python code:</p> <pre><code>import time def f(n): start = time.time() a = u'' #remove u to use non Unicode string for i in xrange(n): a = a + str(i) print len(a), 'time', (time.time() - start)*1000.0 for j in xrange(10): f(1000 * 100) </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.
 

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