2. 减少冗余数据
3. 合理使用copy与deepcopy
import copy
a=range(100000)
%timeit-n10copy.copy(a)# 运行10次 copy.copy(a)
%timeit-n10copy.deepcopy(a)
10loops,best of3:1.55ms per loop
10loops,best of3:151ms per loop
4. 使用dict或set查找元素
a=range(1000)
s=set(a)
d=dict((i,1)foriina)
%timeit-n10000100ind
%timeit-n10000100ins
10000loops,best of3:43.5ns per loop
10000loops,best of3:49.6ns per loop
5. 合理使用生成器(generator)和yield
%timeit-n100a=(iforiinrange(100000))
%timeit-n100b=[iforiinrange(100000)]
100loops,best of3:1.54ms per loop
100loops,best of3:4.56ms per loop
%timeit-n10forxin(iforiinrange(100000)):pass
%timeit-n10forxin[iforiinrange(100000)]:pass
10loops,best of3:6.51ms per loop
10loops,best of3:5.54ms per loop
def yield_func(ls):
foriinls:
yieldi+1
def not_yield_func(ls):
return[i+1foriinls]
ls=range(1000000)
%timeit-n10foriinyield_func(ls):pass
%timeit-n10foriinnot_yield_func(ls):pass
10loops,best of3:63.8ms per loop
10loops,best of3:62.9ms per loop
6. 优化循环
a=range(10000)
size_a=len(a)
%timeit-n1000foriina:k=len(a)
%timeit-n1000foriina:k=size_a
1000loops,best of3:569µsper loop
1000loops,best of3:256µsper loop7. 优化包含多个判断表达式的顺序
a=range(2000)
%timeit-n100[iforiinaif10<i<20or1000<i<2000]
%timeit-n100[iforiinaif1000<i<2000or100<i<20]
%timeit-n100[iforiinaifi%2==0andi>1900]
%timeit-n100[iforiinaifi>1900andi%2==0]
100loops,best of3:287µsper loop
100loops,best of3:214µsper loop
100loops,best of3:128µsper loop
100loops,best of3:56.1µsper loop8. 使用join合并迭代器中的字符串
In[1]:%%timeit
...:s=''
...:foriina:
...: s+=i
...:
10000loops,best of3:59.8µsper loop
In[2]:%%timeit
s=''.join(a)
...:
100000loops,best of3:11.8µsper loop
9. 选择合适的格式化字符方式
s1,s2='ax','bx'
%timeit-n100000'abc%s%s'%(s1,s2)
%timeit-n100000'abc{0}{1}'.format(s1,s2)
%timeit-n100000'abc'+s1+s2
100000loops,best of3:183ns per loop
100000loops,best of3:169ns per loop
100000loops,best of3:103ns per loop
10. 不借助中间变量交换两个变量的值
In[3]:%%timeit-n10000
a,b=1,2
....:c=a;a=b;b=c;
....:
10000loops,best of3:172ns per loop
In[4]:%%timeit-n10000
a,b=1,2
a,b=b,a
....:
10000loops,best of3:86ns per loop
11. 使用if is
a=range(10000)
%timeit-n100[iforiinaifi==True]
%timeit-n100[iforiinaifiisTrue]
100loops,best of3:531µsper loop
100loops,best of3:362µsper loop
12. 使用级联比较x < y < z
x,y,z=1,2,3
%timeit-n1000000ifx<y<z:pass
%timeit-n1000000ifx<yandy<z:pass
1000000loops,best of3:101ns per loop
1000000loops,best of3:121ns per loop
13. while 1 比 while True 更快
def while_1():
n=100000
while1:
n-=1
ifn<=0:break
def while_true():
n=100000
whileTrue:
n-=1
ifn<=0:break
m,n=1000000,1000000
%timeit-n100while_1()
%timeit-n100while_true()
100loops,best of3:3.69ms per loop
100loops,best of3:5.61ms per loop
14. 使用**而不是pow
%timeit-n10000c=pow(2,20)
%timeit-n10000c=2**20
10000loops,best of3:284ns per loop
10000loops,best of3:16.9ns per loop
15. 使用 cProfile, cStringIO 和 cPickle等用c实现相同功能(分别对应profile, StringIO, pickle)的包
import cPickle
import pickle
a=range(10000)
%timeit-n100x=cPickle.dumps(a)
%timeit-n100x=pickle.dumps(a)
100loops,best of3:1.58ms per loop
100loops,best of3:17ms per loop
16. 使用最佳的反序列化方式
import json
import cPickle
a=range(10000)
s1=str(a)
s2=cPickle.dumps(a)
s3=json.dumps(a)
%timeit-n100x=eval(s1)
%timeit-n100x=cPickle.loads(s2)
%timeit-n100x=json.loads(s3)
100loops,best of3:16.8ms per loop
100loops,best of3:2.02ms per loop
100loops,best of3:798µsper loop
17. 使用C扩展(Extension)
18. 并行编程
19. 终级大杀器:PyPy
20. 使用性能分析工具
永远不要相信任何人对我们的任何评价,这样的你,不会在不知不觉中跟魔鬼们签下限制自己的契约。......