Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
fd9ebd4a
Kaydet (Commit)
fd9ebd4a
authored
Kas 25, 2011
tarafından
Antoine Pitrou
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Clarify concatenation behaviour of immutable strings, and remove explicit
mention of the CPython optimization hack.
üst
5a53f368
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
9 deletions
+38
-9
programming.rst
Doc/faq/programming.rst
+26
-0
stdtypes.rst
Doc/library/stdtypes.rst
+12
-9
No files found.
Doc/faq/programming.rst
Dosyayı görüntüle @
fd9ebd4a
...
...
@@ -989,6 +989,32 @@ What does 'UnicodeDecodeError' or 'UnicodeEncodeError' error mean?
See the :ref:`unicode-howto`.
What is the most efficient way to concatenate many strings together?
--------------------------------------------------------------------
:class:`str` and :class:`bytes` objects are immutable, therefore concatenating
many strings together is inefficient as each concatenation creates a new
object. In the general case, the total runtime cost is quadratic in the
total string length.
To accumulate many :class:`str` objects, the recommended idiom is to place
them into a list and call :meth:`str.join` at the end::
chunks = []
for s in my_strings:
chunks.append(s)
result = ''.join(chunks)
(another reasonably efficient idiom is to use :class:`io.StringIO`)
To accumulate many :class:`bytes` objects, the recommended idiom is to extend
a :class:`bytearray` object using in-place concatenation (the ``+=`` operator)::
result = bytearray()
for b in my_bytes_objects:
result += b
Sequences (Tuples/Lists)
========================
...
...
Doc/library/stdtypes.rst
Dosyayı görüntüle @
fd9ebd4a
...
...
@@ -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:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment