您当前的位置: 首页 >  Python

Xavier Jiezou

暂无认证

  • 1浏览

    0关注

    394博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Python】选择正确的字符串累加姿势,速度可以提升数倍

Xavier Jiezou 发布时间:2022-05-12 09:43:33 ,浏览量:1

引言

在通读谷歌的 Python 代码规范文档中,发现了有意思的有一段话:

Avoid using the + and += operators to accumulate a string within a loop. In some conditions, accumulating a string with addition can lead to quadratic rather than linear running time. Although common accumulations of this sort may be optimized on CPython, that is an implementation detail. The conditions under which an optimization applies are not easy to predict and may change. Instead, add each substring to a list and ''.join the list after the loop terminates, or write each substring to an io.StringIO buffer. These techniques consistently have amortized-linear run time complexity.

大意就是说,避免在循环中使用 ++= 运算符累积字符串。替代的是,先将字符串放到一个列表 list 中,然后使用 ''.join(list) 的方法来累加字符串。或者你也可以使用 io.StringIO 缓冲。

这是因为在循环中使用 ++= 运算符累积字符串的时间复杂度是 O(n2),而使用 ''.join(list) 的时间复杂度是 O(n)

方法 +/+=
>>> s = ''
>>> for i in ['a', 'b', 'c']:
...     s += i
... 
>>> s
'abc'
.join()
>>> s = ''.join(['a', 'b', 'c']) 
>>> s
'abc'
实验

这里设置一个实验对比 +=.join() 累加字符串耗时。

  • +=
import time
t1 = time.time()
s = ''
for i in [chr(i) for i in range(96, 123)]*1000000:
    s += i
t2 = time.time()
print(f'Time-consuming: {t2-t1}')
  • .join()
import time
t1 = time.time()
s = ''.join([chr(i) for i in range(96, 123)]*1000000)
t2 = time.time()
print(f'Time-consuming: {t2-t1}')

实验结果如下:

类型耗时+=24.35s''.join()0.26s

得出结论:当需要累加的字符串很多时,''.join() 的速度明显更快。

参考

https://google.github.io/styleguide/pyguide.html

关注
打赏
1661408149
查看更多评论
立即登录/注册

微信扫码登录

0.0402s