设为首页 收藏本站
查看: 717|回复: 0

[经验分享] python 性能测试(1)-- % vs + vs str.join vs +=

[复制链接]

尚未签到

发表于 2017-5-5 11:56:35 | 显示全部楼层 |阅读模式
网上广为流传着"不要使用+" 连接字符串的“经验”, 特别是这篇文章当中提到了
http://www.oschina.net/question/129540_61768
引用
要使用 out = "<html>%s%s%s%s</html>" % (head, prologue, query, tail)
而避免 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 的提示,代码更简洁了):

#!/usr/bin/env python
import time

def show_run_time(name, count, func, args):
start = time.time()
for i in xrange(count):
apply(func, [],  args)
print("'%s' run %d times, spent %.6f seconds"%(name, count, time.time()-start))

if __name__ == '__main__':
count = 1000 * 1000
args1 = { #short items
"a": "1"*3,
"b": "2"*4,
"c": "3"*5,
"d": "4"*6,
"e": "5"*7,
"f": "6"*8,
"g": "7"*9,
"h": "8"*10,
}

args2 = { #long items
"a": "1"*101,
"b": "2"*131,
"c": "3"*161,
"d": "4"*191,
"e": "5"*221,
"f": "6"*251,
"g": "7"*281,
"h": "8"*311,
}

def test_str_format(a, b, c, d, e, f, g, h):
out1 = "<html>%s%s%s%s%s%s%s%s</html>" % (a, b, c, d, e, f, g, h)
out2 = "<table>%s%s%s%s%s%s%s%s</table>" % (h, b, c, d, e, f, g, a)
return  ("%s | %s"%(out1, out2))
def test_plus(a, b, c, d, e, f, g, h):
out1 = "<html>" + a + b  + c + d + e + f + g + b + "</html>"
out2 = "<table>" + h + b  + c + d + e + f + g + a + "</table>"
return  out1 + " | " + out2
def test_plus_and_equal(a, b, c, d, e, f, g, h):
out = "<html>"
for i in [a, b, c, d, e, f, g, h]:
out += i
out += "</html> | <table>"
for i in [h, b, c, d, e, f, g, a]:
out += i
out += "</table>"
return out

def test_join(a, b, c, d, e, f, g, h):
out1 = "".join(["<html>", a, b, c, d, e, f, g, h, "</html>"])
out2 = "".join(["<table>", h, b, c, d, e, f, g, a, "</table>"])
return  " | ".join((out1, out2))
print "short items test:"
show_run_time("use % to join", count, test_str_format, args1)
show_run_time("use + ", count, test_plus, args1)
show_run_time("use str.join ", count, test_join, args1)
show_run_time("use += ", count, test_plus_and_equal, args1)
print "-----"
print "long items test:"
show_run_time("use % to join", count, test_str_format, args2)
show_run_time("use + ", count, test_plus, args2)
show_run_time("use str.join ", count, test_join, args2)
show_run_time("use += ", count, test_plus_and_equal, args2)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373453-1-1.html 上篇帖子: Python 3 教程二:文件,目录和路径 下篇帖子: 复制窗口Python for Ulipad(总结ulipad使用) 复制窗口
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表