[pypy-commit] pypy default: issue #2186: implement the missing string concatenation optimization in

cfbolz pypy.commits at gmail.com
Wed Mar 29 06:36:53 EDT 2017


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: 
Changeset: r90858:d52a4942fee0
Date: 2017-03-29 12:34 +0200
http://bitbucket.org/pypy/pypy/changeset/d52a4942fee0/

Log:	issue #2186: implement the missing string concatenation optimization
	in cpython_differences

diff --git a/pypy/doc/cpython_differences.rst b/pypy/doc/cpython_differences.rst
--- a/pypy/doc/cpython_differences.rst
+++ b/pypy/doc/cpython_differences.rst
@@ -357,6 +357,24 @@
 
 .. __: https://bitbucket.org/pypy/pypy/issue/1974/different-behaviour-for-collections-of
 
+Performance Differences
+-------------------------
+
+CPython has an optimization that can make repeated string concatenation not
+quadratic. For example, this kind of code runs in O(n) time::
+
+    s = ''
+    for string in mylist:
+        s += string
+
+In PyPy, this code will always have quadratic complexity. Note also, that the
+CPython optimization is brittle and can break by having slight variations in
+your code anyway. So you should anyway replace the code with::
+
+    parts = []
+    for string in mylist:
+        parts.append(string)
+    s = "".join(parts)
 
 Miscellaneous
 -------------


More information about the pypy-commit mailing list