Python性能调试笔记
::-- Roka [DateTime(2007-04-26T14:46:55Z)] TableOfContents
1. 概要
TODO
1.1. 字符串连接
(1)
普通代码:
1 s = ""
2 for substring in list:
3 s += substring
高性能代码:
(2)
普通代码:
1 s = ""
2 for x in list:
3 s += someFunction(x)
高性能代码:
1 slist = [someFunction(x) for x in somelist]
2 s = "".join(slist)
(3)
普通代码:
1 out = "<html>" + head + prologue + query + tail + "</html>"
高性能代码:
1 out = "<html>%(head)s%(prologue)s%(query)s%(tail)s</html>" % locals()
1.2. 循环
(1)一个转换大写的例程:
普通代码:
1 newlist = []
2 for word in oldlist:
3 newlist.append(word.upper())
高性能代码:
1
2 newlist = map(str.upper, oldlist)
3
4 newlist = [s.upper() for s in oldlist]
5
6 newlist = (s.upper() for s in oldlist)
1.3. 面向对象
(1)假设不能使用map()和list comprehension,你只能使用循环时要避免”带点循环“:
1 upper = str.upper
2 newlist = []
3 append = newlist.append
4
5 for word in list:
6 append(upper(word))
1.4. 本地变量
(1)终极办法-使用本地变量代替全局变量
1 def func():
2 upper = str.upper
3 newlist = []
4 append = newlist.append
5 for word in words:
6 append(upper(word))
7 return newlist
1.5. 字典
(1)不要带IF循环:
普通代码:
1 wdict= {}
2 for word in words:
3 if word not in wdict:
4 wdict[word] = 0
5 wdict[word] += 1
高性能代码:
1
2 wdict = {}
3 for word in words:
4 try:
5 wdict[word] += 1
6 except KeyError:
7 wdict[word] = 1
8
9
10 wdict = {}
11 get = wdict.get
12 for word in words:
13 wdict[word] = get(word, 0) + 1
如果在字典里的是对象或列表,你还可以用dict.setdefault 方法
1 wdict.setdefault(key, []).append(newElement)
1.6. Import
(1)在本地import会比全局import高效。
(2)保证只import一次。
1
2 pack = None
3
4 def parse_pack():
5 global pack
6 if pack is None:
7 import pack
8 ...
1.7. 数据集合处理
(1)避免在循环中进行函数调用
普通代码:
1 import time
2 x = 0
3 def doit(i):
4 global x
5 x = x + 1
6
7 list = range(100000)
8 t = time.time()
9 for i in list:
10 doit(i)
11
12 print "%.3f" %(time.time() -t )
高性能代码:
1 import time
2 x = 0
3 def doit(i):
4 global x
5 for i in list:
6 x = x + 1
7 x = x + 1
8
9 list = range(100000)
10 t = time.time()
11 doit(list)
12
13 print "%.3f" %(time.time() -t )
(什么??竟然快了4倍以上!!)
1.8. 使用xrange()代替range()
毕竟xrange()是C完全实现的。
2. 交流
PageComment2