python 性能测试(1)-- % vs + vs str.join vs +=
网上广为流传着"不要使用+" 连接字符串的“经验”, 特别是这篇文章当中提到了
http://www.oschina.net/question/129540_61768
而避免 out = "<html>" + head + prologue + query + tail + "</html>"
-------
实际上果真如此吗? 连接字符串主要有四种用法:
% +str.join+=
我的理解是这样的
% 和 + 都是应付参数个数已知的、固定的,比如已经知道了a,b,c 三个参数了,那么a+b+c
str.join 和 += 主要是应付参数个数未知的、不固定的,比如参数的一个列表当中所有的字符串,这个列表长度未知;当然他们也可以应付参数个数固定的情况
我的开发环境是:
CPU: Intel(R) Xeon(R) E5520 @ 2.27GHz
Memory: 2G
OS: Ubuntu 12.04
第1回合使用
解释器:python 2.7.3
'use % to join' run 1000000 times, spent 2.045571 seconds
'use + ' run 1000000 times, spent 2.238076 seconds
'use str.join ' run 1000000 times, spent 1.816502 seconds
'use += ' run 1000000 times, spent 3.134726 seconds
-----
long items test:
'use % to join' run 1000000 times, spent 3.631465 seconds
'use + ' run 1000000 times, spent 3.203789 seconds
'use str.join ' run 1000000 times, spent 2.474077 seconds
'use += ' run 1000000 times, spent 3.927131 seconds
第2回合使用 pypy, 鉴于使用pypy的人多了起来, 这回是使用ubuntu12 自带的 pypy 1.8
解释器:pypy 1.8 (== python 2.7.2)
short items test:
'use % to join' run 1000000 times, spent 1.637780 seconds
'use + ' run 1000000 times, spent 1.498548 seconds
'use str.join ' run 1000000 times, spent 2.204772 seconds
'use += ' run 1000000 times, spent 2.955786 seconds
-----
long items test:
'use % to join' run 1000000 times, spent 3.570450 seconds
'use + ' run 1000000 times, spent 1.730548 seconds
'use str.join ' run 1000000 times, spent 2.914669 seconds
'use += ' run 1000000 times, spent 5.924469 seconds
第3回合使用 最新的PyPy 2.0 beta1
解释器:pypy 2.0 beta1 (== python 2.7.3)
short items test:
'use % to join' run 1000000 times, spent 0.749635 seconds
'use + ' run 1000000 times, spent 0.423104 seconds
'use str.join ' run 1000000 times, spent 1.319862 seconds
'use += ' run 1000000 times, spent 1.985512 seconds
-----
long items test:
'use % to join' run 1000000 times, spent 2.512883 seconds
'use + ' run 1000000 times, spent 0.414724 seconds
'use str.join ' run 1000000 times, spent 1.934615 seconds
'use += ' run 1000000 times, spent 4.973543 seconds
可以下个结论了:
在纯python上,无论长字符串还是短字符串: 参数个数已知 or 参数个数未知, 都用join吧
如果是pypy, 无论长字符串还是短字符串: 参数个数已知用+ , 未知用join吧; BTW, pypy 2.0beta 的速度确实挺快的
无论哪种情况+哪种解释器,的确是不要用 "+=" !
我的测试代码如下(特别感谢提醒“暗能量有点甜”http://www.weibo.com/kyuseishu 的提示,代码更简洁了):