- 在使用字符拼接时,str.join() 比 + 速度要快
参考: https://docs.python.org/2.7/library/stdtypes.html
CPython implementation 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 str.join()
method which assures consistent linear concatenation performance across versions and implementations.
网友评论