Nếu bạn chỉ có một tham chiếu đến một chuỗi và bạn kết hợp một chuỗi khác vào cuối, thì CPython hiện các trường hợp đặc biệt này và cố gắng mở rộng chuỗi tại chỗ.
Kết quả cuối cùng là hoạt động được khấu hao o [n].
e.g.
s = ""
for i in range[n]:
s+=str[i]
Đã từng là o [n^2], nhưng bây giờ nó là o [n].
Từ nguồn [byteObject.c]:
void
PyBytes_ConcatAndDel[register PyObject **pv, register PyObject *w]
{
PyBytes_Concat[pv, w];
Py_XDECREF[w];
}
/* The following function breaks the notion that strings are immutable:
it changes the size of a string. We get away with this only if there
is only one module referencing the object. You can also think of it
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
already be known to some other part of the code...
Note that if there's not enough memory to resize the string, the original
string object at *pv is deallocated, *pv is set to NULL, an "out of
memory" exception is set, and -1 is returned. Else [on success] 0 is
returned, and the value in *pv may or may not be the same as on input.
As always, an extra byte is allocated for a trailing \0 byte [newsize
does *not* include that], and a trailing \0 byte is stored.
*/
int
_PyBytes_Resize[PyObject **pv, Py_ssize_t newsize]
{
register PyObject *v;
register PyBytesObject *sv;
v = *pv;
if [!PyBytes_Check[v] || Py_REFCNT[v] != 1 || newsize < 0] {
*pv = 0;
Py_DECREF[v];
PyErr_BadInternalCall[];
return -1;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference[v];
*pv = [PyObject *]
PyObject_REALLOC[[char *]v, PyBytesObject_SIZE + newsize];
if [*pv == NULL] {
PyObject_Del[v];
PyErr_NoMemory[];
return -1;
}
_Py_NewReference[*pv];
sv = [PyBytesObject *] *pv;
Py_SIZE[sv] = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
}
Nó đủ dễ dàng để xác minh theo kinh nghiệm.
$ python -m timeit -s"s=''" "for i in xrange[10]:s+='a'" 1000000 loops, best of 3: 1.85 usec per loop $ python -m timeit -s"s=''" "for i in xrange[100]:s+='a'" 10000 loops, best of 3: 16.8 usec per loop $ python -m timeit -s"s=''" "for i in xrange[1000]:s+='a'" 10000 loops, best of 3: 158 usec per loop $ python -m timeit -s"s=''" "for i in xrange[10000]:s+='a'" 1000 loops, best of 3: 1.71 msec per loop $ python -m timeit -s"s=''" "for i in xrange[100000]:s+='a'" 10 loops, best of 3: 14.6 msec per loop $ python -m timeit -s"s=''" "for i in xrange[1000000]:s+='a'" 10 loops, best of 3: 173 msec per loop
Tuy nhiên, điều quan trọng cần lưu ý là tối ưu hóa này không phải là một phần của thông số Python. Nó chỉ trong việc triển khai Cpython theo như tôi biết. Ví dụ, cùng một thử nghiệm thực nghiệm trên Pypy hoặc Jython có thể hiển thị hiệu suất O [N ** 2] cũ hơn. however to note that this optimisation isn't part of the Python spec. It's only in the cPython implementation as far as I know. The same empirical testing on pypy or jython for example might show the older O[n**2] performance .
$ pypy -m timeit -s"s=''" "for i in xrange[10]:s+='a'" 10000 loops, best of 3: 90.8 usec per loop $ pypy -m timeit -s"s=''" "for i in xrange[100]:s+='a'" 1000 loops, best of 3: 896 usec per loop $ pypy -m timeit -s"s=''" "for i in xrange[1000]:s+='a'" 100 loops, best of 3: 9.03 msec per loop $ pypy -m timeit -s"s=''" "for i in xrange[10000]:s+='a'" 10 loops, best of 3: 89.5 msec per loop
Cho đến nay rất tốt, nhưng sau đó,
$ pypy -m timeit -s"s=''" "for i in xrange[100000]:s+='a'" 10 loops, best of 3: 12.8 sec per loop
ouch thậm chí còn tồi tệ hơn bậc hai. Vì vậy, Pypy đang làm một cái gì đó hoạt động tốt với các chuỗi ngắn, nhưng hoạt động kém cho các chuỗi lớn hơn.
sửa đổi lần cuối ngày 29 tháng 7 năm 2022
Python Thêm hướng dẫn chuỗi cho thấy cách kết hợp các chuỗi trong Python.
Hướng dẫn Python là một hướng dẫn toàn diện về ngôn ngữ Python.
Trong Python, một chuỗi là một chuỗi các ký tự Unicode được đặt hàng.
Có một số cách để thêm chuỗi vào Python:
- + toán tử
- __add__ Phương pháp
- Phương thức tham gia
- Định dạng chuỗi
Python thêm chuỗi với toán tử +
Cách dễ nhất để nối các chuỗi là sử dụng toán tử
void
PyBytes_ConcatAndDel[register PyObject **pv, register PyObject *w]
{
PyBytes_Concat[pv, w];
Py_XDECREF[w];
}
/* The following function breaks the notion that strings are immutable:
it changes the size of a string. We get away with this only if there
is only one module referencing the object. You can also think of it
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
already be known to some other part of the code...
Note that if there's not enough memory to resize the string, the original
string object at *pv is deallocated, *pv is set to NULL, an "out of
memory" exception is set, and -1 is returned. Else [on success] 0 is
returned, and the value in *pv may or may not be the same as on input.
As always, an extra byte is allocated for a trailing \0 byte [newsize
does *not* include that], and a trailing \0 byte is stored.
*/
int
_PyBytes_Resize[PyObject **pv, Py_ssize_t newsize]
{
register PyObject *v;
register PyBytesObject *sv;
v = *pv;
if [!PyBytes_Check[v] || Py_REFCNT[v] != 1 || newsize < 0] {
*pv = 0;
Py_DECREF[v];
PyErr_BadInternalCall[];
return -1;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference[v];
*pv = [PyObject *]
PyObject_REALLOC[[char *]v, PyBytesObject_SIZE + newsize];
if [*pv == NULL] {
PyObject_Del[v];
PyErr_NoMemory[];
return -1;
}
_Py_NewReference[*pv];
sv = [PyBytesObject *] *pv;
Py_SIZE[sv] = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
}
4 hoặc void
PyBytes_ConcatAndDel[register PyObject **pv, register PyObject *w]
{
PyBytes_Concat[pv, w];
Py_XDECREF[w];
}
/* The following function breaks the notion that strings are immutable:
it changes the size of a string. We get away with this only if there
is only one module referencing the object. You can also think of it
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
already be known to some other part of the code...
Note that if there's not enough memory to resize the string, the original
string object at *pv is deallocated, *pv is set to NULL, an "out of
memory" exception is set, and -1 is returned. Else [on success] 0 is
returned, and the value in *pv may or may not be the same as on input.
As always, an extra byte is allocated for a trailing \0 byte [newsize
does *not* include that], and a trailing \0 byte is stored.
*/
int
_PyBytes_Resize[PyObject **pv, Py_ssize_t newsize]
{
register PyObject *v;
register PyBytesObject *sv;
v = *pv;
if [!PyBytes_Check[v] || Py_REFCNT[v] != 1 || newsize < 0] {
*pv = 0;
Py_DECREF[v];
PyErr_BadInternalCall[];
return -1;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference[v];
*pv = [PyObject *]
PyObject_REALLOC[[char *]v, PyBytesObject_SIZE + newsize];
if [*pv == NULL] {
PyObject_Del[v];
PyErr_NoMemory[];
return -1;
}
_Py_NewReference[*pv];
sv = [PyBytesObject *] *pv;
Py_SIZE[sv] = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
}
5. Toán tử void
PyBytes_ConcatAndDel[register PyObject **pv, register PyObject *w]
{
PyBytes_Concat[pv, w];
Py_XDECREF[w];
}
/* The following function breaks the notion that strings are immutable:
it changes the size of a string. We get away with this only if there
is only one module referencing the object. You can also think of it
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
already be known to some other part of the code...
Note that if there's not enough memory to resize the string, the original
string object at *pv is deallocated, *pv is set to NULL, an "out of
memory" exception is set, and -1 is returned. Else [on success] 0 is
returned, and the value in *pv may or may not be the same as on input.
As always, an extra byte is allocated for a trailing \0 byte [newsize
does *not* include that], and a trailing \0 byte is stored.
*/
int
_PyBytes_Resize[PyObject **pv, Py_ssize_t newsize]
{
register PyObject *v;
register PyBytesObject *sv;
v = *pv;
if [!PyBytes_Check[v] || Py_REFCNT[v] != 1 || newsize < 0] {
*pv = 0;
Py_DECREF[v];
PyErr_BadInternalCall[];
return -1;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference[v];
*pv = [PyObject *]
PyObject_REALLOC[[char *]v, PyBytesObject_SIZE + newsize];
if [*pv == NULL] {
PyObject_Del[v];
PyErr_NoMemory[];
return -1;
}
_Py_NewReference[*pv];
sv = [PyBytesObject *] *pv;
Py_SIZE[sv] = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
}
4 được sử dụng cả để thêm số và chuỗi; Trong lập trình, chúng tôi nói rằng toán tử bị quá tải.add_string.py
#!/usr/bin/python a = 'old' b = ' tree' c = a + b print[c]
Hai chuỗi được thêm vào bằng toán tử
void
PyBytes_ConcatAndDel[register PyObject **pv, register PyObject *w]
{
PyBytes_Concat[pv, w];
Py_XDECREF[w];
}
/* The following function breaks the notion that strings are immutable:
it changes the size of a string. We get away with this only if there
is only one module referencing the object. You can also think of it
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
already be known to some other part of the code...
Note that if there's not enough memory to resize the string, the original
string object at *pv is deallocated, *pv is set to NULL, an "out of
memory" exception is set, and -1 is returned. Else [on success] 0 is
returned, and the value in *pv may or may not be the same as on input.
As always, an extra byte is allocated for a trailing \0 byte [newsize
does *not* include that], and a trailing \0 byte is stored.
*/
int
_PyBytes_Resize[PyObject **pv, Py_ssize_t newsize]
{
register PyObject *v;
register PyBytesObject *sv;
v = *pv;
if [!PyBytes_Check[v] || Py_REFCNT[v] != 1 || newsize < 0] {
*pv = 0;
Py_DECREF[v];
PyErr_BadInternalCall[];
return -1;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference[v];
*pv = [PyObject *]
PyObject_REALLOC[[char *]v, PyBytesObject_SIZE + newsize];
if [*pv == NULL] {
PyObject_Del[v];
PyErr_NoMemory[];
return -1;
}
_Py_NewReference[*pv];
sv = [PyBytesObject *] *pv;
Py_SIZE[sv] = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
}
4.$ ./add_string.py old tree
Trong ví dụ thứ hai, chúng tôi sử dụng toán tử bổ sung hợp chất.
add_string2.py
#!/usr/bin/python msg = 'There are' msg += ' three falcons' msg += ' in the sky' print[msg]
Ví dụ xây dựng một thông điệp với toán tử
void
PyBytes_ConcatAndDel[register PyObject **pv, register PyObject *w]
{
PyBytes_Concat[pv, w];
Py_XDECREF[w];
}
/* The following function breaks the notion that strings are immutable:
it changes the size of a string. We get away with this only if there
is only one module referencing the object. You can also think of it
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
already be known to some other part of the code...
Note that if there's not enough memory to resize the string, the original
string object at *pv is deallocated, *pv is set to NULL, an "out of
memory" exception is set, and -1 is returned. Else [on success] 0 is
returned, and the value in *pv may or may not be the same as on input.
As always, an extra byte is allocated for a trailing \0 byte [newsize
does *not* include that], and a trailing \0 byte is stored.
*/
int
_PyBytes_Resize[PyObject **pv, Py_ssize_t newsize]
{
register PyObject *v;
register PyBytesObject *sv;
v = *pv;
if [!PyBytes_Check[v] || Py_REFCNT[v] != 1 || newsize < 0] {
*pv = 0;
Py_DECREF[v];
PyErr_BadInternalCall[];
return -1;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference[v];
*pv = [PyObject *]
PyObject_REALLOC[[char *]v, PyBytesObject_SIZE + newsize];
if [*pv == NULL] {
PyObject_Del[v];
PyErr_NoMemory[];
return -1;
}
_Py_NewReference[*pv];
sv = [PyBytesObject *] *pv;
Py_SIZE[sv] = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
}
5.$ ./add_string2.py There are three falcons in the sky
Python thêm chuỗi với tham gia
Phương thức chuỗi
void
PyBytes_ConcatAndDel[register PyObject **pv, register PyObject *w]
{
PyBytes_Concat[pv, w];
Py_XDECREF[w];
}
/* The following function breaks the notion that strings are immutable:
it changes the size of a string. We get away with this only if there
is only one module referencing the object. You can also think of it
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
already be known to some other part of the code...
Note that if there's not enough memory to resize the string, the original
string object at *pv is deallocated, *pv is set to NULL, an "out of
memory" exception is set, and -1 is returned. Else [on success] 0 is
returned, and the value in *pv may or may not be the same as on input.
As always, an extra byte is allocated for a trailing \0 byte [newsize
does *not* include that], and a trailing \0 byte is stored.
*/
int
_PyBytes_Resize[PyObject **pv, Py_ssize_t newsize]
{
register PyObject *v;
register PyBytesObject *sv;
v = *pv;
if [!PyBytes_Check[v] || Py_REFCNT[v] != 1 || newsize < 0] {
*pv = 0;
Py_DECREF[v];
PyErr_BadInternalCall[];
return -1;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference[v];
*pv = [PyObject *]
PyObject_REALLOC[[char *]v, PyBytesObject_SIZE + newsize];
if [*pv == NULL] {
PyObject_Del[v];
PyErr_NoMemory[];
return -1;
}
_Py_NewReference[*pv];
sv = [PyBytesObject *] *pv;
Py_SIZE[sv] = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
}
9 kết hợp bất kỳ số lượng chuỗi nào được cung cấp trong một điều không thể điều chỉnh được [danh sách, danh sách]. Chúng tôi chỉ định ký tự mà các chuỗi được nối.add_string_join.py
#!/usr/bin/python msg = ' '.join[['There', 'are', 'three', 'eagles', 'in', 'the', 'sky']] print[msg]
Trong ví dụ, chúng tôi tạo thành một thông điệp bằng cách tham gia bảy từ. Các từ được nối với một ký tự không gian duy nhất.
void
PyBytes_ConcatAndDel[register PyObject **pv, register PyObject *w]
{
PyBytes_Concat[pv, w];
Py_XDECREF[w];
}
/* The following function breaks the notion that strings are immutable:
it changes the size of a string. We get away with this only if there
is only one module referencing the object. You can also think of it
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
already be known to some other part of the code...
Note that if there's not enough memory to resize the string, the original
string object at *pv is deallocated, *pv is set to NULL, an "out of
memory" exception is set, and -1 is returned. Else [on success] 0 is
returned, and the value in *pv may or may not be the same as on input.
As always, an extra byte is allocated for a trailing \0 byte [newsize
does *not* include that], and a trailing \0 byte is stored.
*/
int
_PyBytes_Resize[PyObject **pv, Py_ssize_t newsize]
{
register PyObject *v;
register PyBytesObject *sv;
v = *pv;
if [!PyBytes_Check[v] || Py_REFCNT[v] != 1 || newsize < 0] {
*pv = 0;
Py_DECREF[v];
PyErr_BadInternalCall[];
return -1;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference[v];
*pv = [PyObject *]
PyObject_REALLOC[[char *]v, PyBytesObject_SIZE + newsize];
if [*pv == NULL] {
PyObject_Del[v];
PyErr_NoMemory[];
return -1;
}
_Py_NewReference[*pv];
sv = [PyBytesObject *] *pv;
Py_SIZE[sv] = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
}
0Python thêm chuỗi với định dạng chuỗi
Chúng ta có thể xây dựng chuỗi Python với định dạng chuỗi. Các biến được mở rộng trong các ký tự
$ python -m timeit -s"s=''" "for i in xrange[10]:s+='a'" 1000000 loops, best of 3: 1.85 usec per loop $ python -m timeit -s"s=''" "for i in xrange[100]:s+='a'" 10000 loops, best of 3: 16.8 usec per loop $ python -m timeit -s"s=''" "for i in xrange[1000]:s+='a'" 10000 loops, best of 3: 158 usec per loop $ python -m timeit -s"s=''" "for i in xrange[10000]:s+='a'" 1000 loops, best of 3: 1.71 msec per loop $ python -m timeit -s"s=''" "for i in xrange[100000]:s+='a'" 10 loops, best of 3: 14.6 msec per loop $ python -m timeit -s"s=''" "for i in xrange[1000000]:s+='a'" 10 loops, best of 3: 173 msec per loop0 bên trong chuỗi.
format_str.py
void
PyBytes_ConcatAndDel[register PyObject **pv, register PyObject *w]
{
PyBytes_Concat[pv, w];
Py_XDECREF[w];
}
/* The following function breaks the notion that strings are immutable:
it changes the size of a string. We get away with this only if there
is only one module referencing the object. You can also think of it
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
already be known to some other part of the code...
Note that if there's not enough memory to resize the string, the original
string object at *pv is deallocated, *pv is set to NULL, an "out of
memory" exception is set, and -1 is returned. Else [on success] 0 is
returned, and the value in *pv may or may not be the same as on input.
As always, an extra byte is allocated for a trailing \0 byte [newsize
does *not* include that], and a trailing \0 byte is stored.
*/
int
_PyBytes_Resize[PyObject **pv, Py_ssize_t newsize]
{
register PyObject *v;
register PyBytesObject *sv;
v = *pv;
if [!PyBytes_Check[v] || Py_REFCNT[v] != 1 || newsize < 0] {
*pv = 0;
Py_DECREF[v];
PyErr_BadInternalCall[];
return -1;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference[v];
*pv = [PyObject *]
PyObject_REALLOC[[char *]v, PyBytesObject_SIZE + newsize];
if [*pv == NULL] {
PyObject_Del[v];
PyErr_NoMemory[];
return -1;
}
_Py_NewReference[*pv];
sv = [PyBytesObject *] *pv;
Py_SIZE[sv] = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
}
1Chúng tôi xây dựng một thông điệp với fstring của Python.
void
PyBytes_ConcatAndDel[register PyObject **pv, register PyObject *w]
{
PyBytes_Concat[pv, w];
Py_XDECREF[w];
}
/* The following function breaks the notion that strings are immutable:
it changes the size of a string. We get away with this only if there
is only one module referencing the object. You can also think of it
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
already be known to some other part of the code...
Note that if there's not enough memory to resize the string, the original
string object at *pv is deallocated, *pv is set to NULL, an "out of
memory" exception is set, and -1 is returned. Else [on success] 0 is
returned, and the value in *pv may or may not be the same as on input.
As always, an extra byte is allocated for a trailing \0 byte [newsize
does *not* include that], and a trailing \0 byte is stored.
*/
int
_PyBytes_Resize[PyObject **pv, Py_ssize_t newsize]
{
register PyObject *v;
register PyBytesObject *sv;
v = *pv;
if [!PyBytes_Check[v] || Py_REFCNT[v] != 1 || newsize < 0] {
*pv = 0;
Py_DECREF[v];
PyErr_BadInternalCall[];
return -1;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference[v];
*pv = [PyObject *]
PyObject_REALLOC[[char *]v, PyBytesObject_SIZE + newsize];
if [*pv == NULL] {
PyObject_Del[v];
PyErr_NoMemory[];
return -1;
}
_Py_NewReference[*pv];
sv = [PyBytesObject *] *pv;
Py_SIZE[sv] = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
}
2Python thêm chuỗi với __add_ phương thức
Một khả năng khác để thêm chuỗi là sử dụng phương pháp
$ python -m timeit -s"s=''" "for i in xrange[10]:s+='a'" 1000000 loops, best of 3: 1.85 usec per loop $ python -m timeit -s"s=''" "for i in xrange[100]:s+='a'" 10000 loops, best of 3: 16.8 usec per loop $ python -m timeit -s"s=''" "for i in xrange[1000]:s+='a'" 10000 loops, best of 3: 158 usec per loop $ python -m timeit -s"s=''" "for i in xrange[10000]:s+='a'" 1000 loops, best of 3: 1.71 msec per loop $ python -m timeit -s"s=''" "for i in xrange[100000]:s+='a'" 10 loops, best of 3: 14.6 msec per loop $ python -m timeit -s"s=''" "for i in xrange[1000000]:s+='a'" 10 loops, best of 3: 173 msec per loop1 Dunder đặc biệt.
add_string3.py
void
PyBytes_ConcatAndDel[register PyObject **pv, register PyObject *w]
{
PyBytes_Concat[pv, w];
Py_XDECREF[w];
}
/* The following function breaks the notion that strings are immutable:
it changes the size of a string. We get away with this only if there
is only one module referencing the object. You can also think of it
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
already be known to some other part of the code...
Note that if there's not enough memory to resize the string, the original
string object at *pv is deallocated, *pv is set to NULL, an "out of
memory" exception is set, and -1 is returned. Else [on success] 0 is
returned, and the value in *pv may or may not be the same as on input.
As always, an extra byte is allocated for a trailing \0 byte [newsize
does *not* include that], and a trailing \0 byte is stored.
*/
int
_PyBytes_Resize[PyObject **pv, Py_ssize_t newsize]
{
register PyObject *v;
register PyBytesObject *sv;
v = *pv;
if [!PyBytes_Check[v] || Py_REFCNT[v] != 1 || newsize < 0] {
*pv = 0;
Py_DECREF[v];
PyErr_BadInternalCall[];
return -1;
}
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference[v];
*pv = [PyObject *]
PyObject_REALLOC[[char *]v, PyBytesObject_SIZE + newsize];
if [*pv == NULL] {
PyObject_Del[v];
PyErr_NoMemory[];
return -1;
}
_Py_NewReference[*pv];
sv = [PyBytesObject *] *pv;
Py_SIZE[sv] = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
}
3Ví dụ thêm hai chuỗi với
$ python -m timeit -s"s=''" "for i in xrange[10]:s+='a'" 1000000 loops, best of 3: 1.85 usec per loop $ python -m timeit -s"s=''" "for i in xrange[100]:s+='a'" 10000 loops, best of 3: 16.8 usec per loop $ python -m timeit -s"s=''" "for i in xrange[1000]:s+='a'" 10000 loops, best of 3: 158 usec per loop $ python -m timeit -s"s=''" "for i in xrange[10000]:s+='a'" 1000 loops, best of 3: 1.71 msec per loop $ python -m timeit -s"s=''" "for i in xrange[100000]:s+='a'" 10 loops, best of 3: 14.6 msec per loop $ python -m timeit -s"s=''" "for i in xrange[1000000]:s+='a'" 10 loops, best of 3: 173 msec per loop1.
Trong hướng dẫn này, chúng tôi đã chỉ ra một số cách để thêm chuỗi vào Python.
Đọc hướng dẫn Python hoặc liệt kê tất cả các hướng dẫn Python.