diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-11-25 15:33:53 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-11-25 15:33:53 (GMT) |
commit | fd9ebd4a361805607baea3e038652f207575ced8 (patch) | |
tree | 4ab36059698f9ebb40ea9164f83571b1f380e1f8 /Doc/library | |
parent | 5a53f368e61a5535571362e36c451827ee7d3a27 (diff) | |
download | cpython-fd9ebd4a361805607baea3e038652f207575ced8.zip cpython-fd9ebd4a361805607baea3e038652f207575ced8.tar.gz cpython-fd9ebd4a361805607baea3e038652f207575ced8.tar.bz2 |
Clarify concatenation behaviour of immutable strings, and remove explicit
mention of the CPython optimization hack.
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/stdtypes.rst | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index af1e44a..5b54b09 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -964,15 +964,18 @@ Notes: If *k* is ``None``, it is treated like ``1``. (6) - .. impl-detail:: - - If *s* and *t* are both strings, some Python implementations such as - CPython can usually perform an in-place optimization for assignments of - the form ``s = s + t`` or ``s += t``. When applicable, this optimization - makes quadratic run-time much less likely. This optimization is both - version and implementation dependent. For performance sensitive code, it - is preferable to use the :meth:`str.join` method which assures consistent - linear concatenation performance across versions and implementations. + Concatenating immutable strings always results in a new object. This means + that building up a string by repeated concatenation will have a quadratic + runtime cost in the total string length. To get a linear runtime cost, + you must switch to one of the alternatives below: + + * if concatenating :class:`str` objects, you can build a list and use + :meth:`str.join` at the end; + + * if concatenating :class:`bytes` objects, you can similarly use + :meth:`bytes.join`, or you can do in-place concatenation with a + :class:`bytearray` object. :class:`bytearray` objects are mutable and + have an efficient overallocation mechanism. .. _string-methods: |