python性能优化指南

来源:互联网 发布:大数据 云计算 关系 编辑:程序博客网 时间:2024/06/05 02:18

本文用以记录在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.appendupper = str.upperfor 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] = 0from collections import defaultdictdef f8(oldlist):    d = defaultdict(int)    for string in oldlist:        d[string] += 1
0 0