1.最佳化演演算法時間複雜度
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 loop
7. 最佳化包含多個判斷運算式的順序
a=range(2000)
%timeit-n100[iforiinaif1020or10002000]
%timeit-n100[iforiinaif10002000or10020]
%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 loop
8. 使用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-n1000000ifxpass
%timeit-n1000000ifxpass
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. 使用效能分析工具
《Linux雲端計算及運維架構師高薪實戰班》2018年09月16日即將開課中,120天衝擊Linux運維年薪30萬,改變速約~~~~
*宣告:推送內容及圖片來源於網路,部分內容會有所改動,版權歸原作者所有,如來源資訊有誤或侵犯權益,請聯絡我們刪除或授權事宜。
– END –
更多Linux好文請點選【閱讀原文】哦
↓↓↓