2018-04-10 讨论下str.join()与+

2018-04-10  本文已影响4人  开子的私家地
  1. 在使用字符拼接时,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.

上一篇下一篇

猜你喜欢

热点阅读