这与就地使用无关+=
versus +
二进制添加。你没有告诉我们整个故事。您的原始版本连接了 3 个字符串,而不仅仅是两个:
sTable = sTable + '\n' + sRow # simplified, sRow is a function call
Python 尝试帮助并优化字符串连接;两者在使用时strobj += otherstrobj
and strobj = strobj + otherstringobj
,但是当涉及超过 2 个字符串时,它无法应用此优化。
Python 字符串是不可变的normally,但如果没有其他对左侧字符串对象的引用and无论如何它都会被反弹,然后Python会作弊并且改变字符串。这避免了每次连接时都必须创建新字符串,这可以大大提高速度。
这是在字节码评估循环中实现的。使用时两者都BINARY_ADD在两根弦上 http://hg.python.org/cpython/file/23a60d89dbd4/Python/ceval.c#l1202以及使用时INPLACE_ADD在两根弦上 http://hg.python.org/cpython/file/23a60d89dbd4/Python/ceval.c#l1406, Python 将串联委托给特殊的辅助函数string_concatenate() http://hg.python.org/cpython/file/23a60d89dbd4/Python/ceval.c#l4507。为了能够通过改变字符串来优化串联,首先需要确保该字符串没有其他引用;如果只有堆栈和原始变量引用它那么就可以做到这一点,and the next操作将替换原始变量引用。
因此,如果只有 2 个对该字符串的引用,并且下一个运算符是其中之一STORE_FAST
(设置局部变量),STORE_DEREF
(设置由封闭函数引用的变量)或STORE_NAME
(设置一个全局变量),并且受影响的变量当前引用相同的字符串,然后清除该目标变量以将堆栈的引用数量减少到 1。
这就是为什么您的原始代码无法充分使用此优化的原因。你的表达的第一部分是sTable + '\n'
和next操作是another BINARY_ADD
:
>>> import dis
>>> dis.dis(compile(r"sTable = sTable + '\n' + sRow", '<stdin>', 'exec'))
1 0 LOAD_NAME 0 (sTable)
3 LOAD_CONST 0 ('\n')
6 BINARY_ADD
7 LOAD_NAME 1 (sRow)
10 BINARY_ADD
11 STORE_NAME 0 (sTable)
14 LOAD_CONST 1 (None)
17 RETURN_VALUE
首先BINARY_ADD
后面跟着一个LOAD_NAME
访问sRow
变量,而不是存储操作。这第一BINARY_ADD
必须始终产生一个新的字符串对象,该对象更大sTable
增长并且创建这个新的字符串对象需要越来越多的时间。
您将此代码更改为:
sTable += '\n%s' % sRow
which 删除了第二个串联。现在字节码是:
>>> dis.dis(compile(r"sTable += '\n%s' % sRow", '<stdin>', 'exec'))
1 0 LOAD_NAME 0 (sTable)
3 LOAD_CONST 0 ('\n%s')
6 LOAD_NAME 1 (sRow)
9 BINARY_MODULO
10 INPLACE_ADD
11 STORE_NAME 0 (sTable)
14 LOAD_CONST 1 (None)
17 RETURN_VALUE
我们剩下的就是INPLACE_ADD
接下来是一家商店。现在sTable
可以就地更改,不会导致更大的新字符串对象。
你会得到相同的速度差异:
sTable = sTable + ('\n%s' % sRow)
here.
计时赛显示了差异:
>>> import random
>>> from timeit import timeit
>>> testlist = [''.join([chr(random.randint(48, 127)) for _ in range(random.randrange(10, 30))]) for _ in range(1000)]
>>> def str_threevalue_concat(lst):
... res = ''
... for elem in lst:
... res = res + '\n' + elem
...
>>> def str_twovalue_concat(lst):
... res = ''
... for elem in lst:
... res = res + ('\n%s' % elem)
...
>>> timeit('f(l)', 'from __main__ import testlist as l, str_threevalue_concat as f', number=10000)
6.196403980255127
>>> timeit('f(l)', 'from __main__ import testlist as l, str_twovalue_concat as f', number=10000)
2.3599119186401367
这个故事的寓意是,您首先不应该使用字符串连接。从其他字符串的负载构建新字符串的正确方法是使用列表,然后使用str.join()
:
table_rows = []
for something in something_else:
table_rows += ['\n', GetRow()]
sTable = ''.join(table_rows)
这仍然更快:
>>> def str_join_concat(lst):
... res = ''.join(['\n%s' % elem for elem in lst])
...
>>> timeit('f(l)', 'from __main__ import testlist as l, str_join_concat as f', number=10000)
1.7978830337524414
但你不能仅仅使用'\n'.join(lst)
:
>>> timeit('f(l)', 'from __main__ import testlist as l, nl_join_concat as f', number=10000)
0.23735499382019043