python程式效能優化
阿新 • • 發佈:2018-10-31
最近工作中有個任務,就是優化一個模型的實時性。從有到無,主要完成了以下內容。
0.模型的邏輯
1.演算法邏輯
2.程式碼重構
3.程式的效能優化,包括編譯、多執行緒、多程序、numba
4.語言
numba包,經測試,比較適用於陣列、矩陣等數值計算,其他的型別操作,容易報錯。
from multiprocessing import Pool from functools import partial def math_get(a, b): print(a) return b + a def processing(b): pool = Pool(3) a = [i for i in range(3)] d = pool.map(partial(math_get,b = b), a,) #多個引數代入的時候,採用partial的方式, #其中a作為一個可迭代的物件通過map代入math_get函式 print(d) pool.close() pool.join() if __name__ == "__main__": processing(5)
#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'Great' from multiprocessing import Pool import time def a(num): list_num = [] print(num) for i in num: list_num.append(i) print(i) print(list_num) return list_num if __name__ == "__main__": start = time.time() pool = Pool(3)#程序池 num = [1,2,3,4,5,6] #pool.apply_async(a,args=(num,)) a = pool.map(a,(num, )) print('a',a) pool.close() pool.join() end = time.time() print(end-start)
#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'Great' from multiprocessing import Pool import time from functools import partial def a(num, ccc): list_num = [] for i in num: #list_num.append(i) print(i + ccc) #print(list_num) #return list_num if __name__ == "__main__": start = time.time() pool = Pool(3) num = [1,2,3,4,5,6] ccc = 1 #for i in range(10): #pool.apply_async(a,args=(num,)) pool.map(partial(a,ccc = ccc),(num,))#多個引數 #print('a',a) pool.close() pool.join() end = time.time() print(end-start)