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.
Lệnh bên dưới thực thi lệnh mã trên.
Đầu ra của lệnh thực thi được hiển thị bên dưới. Một ngoại lệ xảy ra cho thấy rằng chuỗi và int không thể được thêm vào
Ngoại lệ: str +int
Cách đúng là sử dụng phương thức STR để chuyển đổi int thành một chuỗi. Đoạn mã bằng phương pháp STR được hiển thị bên dưới. Trục vào Int Int
int vào chuỗi
Nhà điều hành 2,4 %
Toán tử ’%, được sử dụng để nối các chuỗi trong một chuỗi bằng cú pháp định dạng. Đoạn mã để sử dụng toán tử ‘%, được hiển thị bên dưới.% Toán tử
+ Toán tử
str1 = "Hello" str2 = "World" space = " " print[str1 + space + str2]
Lệnh bên dưới cố gắng thực thi đoạn mã trên.
Chạy lệnh
python python_string_append.py
Đầu ra của lệnh thực thi được hiển thị bên dưới.
2.2 * Toán tử
Toán tử**tham gia chuỗi với int. Đoạn mã để sử dụng toán tử ‘**được hiển thị bên dưới.
* Nhà điều hành
print['black' * 5]
Lệnh bên dưới thực thi đoạn mã trên.
Chạy lệnh
python python_string_append.py
Đầu ra của lệnh thực thi được hiển thị bên dưới.
2.2 * Toán tử
Toán tử**tham gia chuỗi với int. Đoạn mã để sử dụng toán tử ‘**được hiển thị bên dưới.
* Nhà điều hành
print['green' + 8]
Lệnh bên dưới thực thi đoạn mã trên.Run Command
python python_string_append.py
* nhà điều hành
Cách đúng là sử dụng phương thức STR để chuyển đổi int thành một chuỗi. Đoạn mã bằng phương pháp STR được hiển thị bên dưới. Trục vào Int Intstring appending to int
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;
}
1Lệnh bên dưới thực thi lệnh mã trên.Run Command
python python_string_append.py
Đầu ra của lệnh thực thi được hiển thị bên dưới.
Nhà điều hành 2,4 %
Toán tử ’%, được sử dụng để nối các chuỗi trong một chuỗi bằng cú pháp định dạng. Đoạn mã để sử dụng toán tử ‘%, được hiển thị bên dưới.% Toán tử% operator
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;
}
3Lệnh bên dưới thực thi lệnh mã trên.Run Command
python python_string_append.py
Đầu ra của lệnh thực thi được hiển thị bên dưới.
Nhà điều hành 2,4 %
Toán tử ’%, được sử dụng để nối các chuỗi trong một chuỗi bằng cú pháp định dạng. Đoạn mã để sử dụng toán tử ‘%, được hiển thị bên dưới.% Toán tửformat operator
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;
}
5Lệnh bên dưới thực thi lệnh mã trên.Run Command
python python_string_append.py
Đầu ra của lệnh thực thi được hiển thị bên dưới.
Nhà điều hành 2,4 %
Toán tử ’%, được sử dụng để nối các chuỗi trong một chuỗi bằng cú pháp định dạng. Đoạn mã để sử dụng toán tử ‘%, được hiển thị bên dưới.% Toán tửJoin method
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;
}
7Lệnh bên dưới thực thi lệnh mã trên.Run Command
python python_string_append.py
Đầu ra của lệnh thực thi được hiển thị bên dưới.
Nhà điều hành 2,4 %Join method
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;
}
9Lệnh bên dưới thực thi lệnh mã trên.Run Command
python python_string_append.py
Đầu ra của lệnh thực thi được hiển thị bên dưới.
Nhà điều hành 2,4 %
Toán tử ’%, được sử dụng để nối các chuỗi trong một chuỗi bằng cú pháp định dạng. Đoạn mã để sử dụng toán tử ‘%, được hiển thị bên dưới.% Toán tửConcatenate with space
$ 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
Lệnh bên dưới thực thi lệnh mã trên.Run Command
python python_string_append.py
Đầu ra của lệnh thực thi được hiển thị bên dưới.
Nhà điều hành 2,4 %
Toán tử ’%, được sử dụng để nối các chuỗi trong một chuỗi bằng cú pháp định dạng. Đoạn mã để sử dụng toán tử ‘%, được hiển thị bên dưới.% Toán tửString io
$ 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 loop3
Lệnh bên dưới thực thi lệnh mã trên.Run Command
$ 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 loop4
Đầu ra của lệnh thực thi được hiển thị bên dưới.
Phương pháp định dạng 2.5
Toán tử định dạng được sử dụng để nối các chuỗi trong một chuỗi như được hiển thị trong đoạn mã bên dưới.format Toán tửReading a log file
$ 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 loop5
Phương pháp định dạnglog.txt
$ 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 loop6
Lệnh bên dưới thực thi lệnh mã trên.Run Command
$ 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 loop7
Đầu ra của lệnh thực thi được hiển thị bên dưới.
Một cách khác để nối các chuỗi là bằng cách tạo một danh sách và thêm chuỗi vào danh sách. Phương pháp tham gia hợp nhất các chuỗi với nhau để có được kết quả. Đoạn mã bằng cách sử dụng phương thức tham gia được hiển thị bên dưới.Join Phương thức
Phương thức tham gia
Một ví dụ khác hiển thị phương thức nối nối các chuỗi nối trong một mảng được trình bày dưới đây. Phương thức