python性能优化指南

本文用以记录在python开发中遇到的性能提高技巧
持续更新中…

1.字符串

在python中string对象是不可变的,而字符串的相加会产生新的字符串。
当需要迭代生成一个长字符串时逐一相加不仅会影响速度也会而外增加内存消耗(如中间结果,参考java StringBuffer), 但是当仅需链接很少的字符串时join方法未必明智

  • join的恰当使用
  • “”%()
  • format比较慢

避免

s = ""
for x in list1:
    s += x

推荐

s.join(list1)

避免

s = ""
for x in list1:
    s += fun(x)

推荐

list2 = [ fun(x) for x in list1 ]
s = "".join(list2)

避免

out = "<html>" + head + prologue + query + tail + "</html>"

建议

out = "<html>%(head)s%(prologue)s%(query)s%(tail)s</html>" % locals()
#变量相加过多时此方法较优,反之较差

2.循环

python解释器在解释for循环时会有较大的性能损耗,如果可以建议多使用列表解析来代替for循环,另外迭代器也在优化时间外还能够减少内存开销,在调用自写函数时map也会提高程序性能

newlist = [s.upper() for s in oldlist]

iterator = (s.upper() for s in oldlist)
newlist = list(iterator)

在循环中尽量避免 .(点号操作符)的使用,因为这往往会增加调用开销
避免

newlist = []
for x in oldlist:
    newlist.append(x.upper())

建议

newlist = []
append = newlist.append
upper = str.upper
for x in oldlist:
    append(upper(x))

3.变量

在函数中尽量使用局部变量,因为调用解释器会先搜索局部变量再搜索全局变量

避免

def f1():
    newlist = []
    append = newlist.append
    upper = str.upper
    global oldlist
    for x in oldlist:
        append(tool1(x))
    return newlist

建议

def f2(oldlist):
    newlist = []
    append = newlist.append
    for x in oldlist:
        append(tool1(x))
    return newlist

4.字典

针对无初值的情况进行优化(提升并不多20%左右,但在一定程度上降低了可读性)

原始方法

def f5(oldlist):
    d = {}
    for string in oldlist:
        if string not in d:
            d[string] = 0
        else:
            d[string] += 1

优化方法

def f6(oldlist):
    d = {}
    for string in oldlist:
        try:
            d[string] += 1
        except KeyError:
            d[string] = 0

from collections import defaultdict
def f8(oldlist):
    d = defaultdict(int)
    for string in oldlist:
        d[string] += 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值