python 性能優(yōu)化方法小結
提高性能有如下方法
1、Cython,用于合并python和c語言靜態(tài)編譯泛型
2、IPython.parallel,用于在本地或者集群上并行執(zhí)行代碼
3、numexpr,用于快速數(shù)值運算
4、multiprocessing,python內(nèi)建的并行處理模塊
5、Numba,用于為cpu動態(tài)編譯python代碼
6、NumbaPro,用于為多核cpu和gpu動態(tài)編譯python代碼
為了驗證相同算法在上面不同實現(xiàn)上的的性能差異,我們先定義一個測試性能的函數(shù)
def perf_comp_data(func_list, data_list, rep=3, number=1): '''Function to compare the performance of different functions. Parameters func_list : list list with function names as strings data_list : list list with data set names as strings rep : int number of repetitions of the whole comparison number : int number ofexecutions for every function ''' from timeit import repeat res_list = {} for name in enumerate(func_list): stmt = name[1] + '(' + data_list[name[0]] + ')' setup = "from __main__ import " + name[1] + ','+ data_list[name[0]] results = repeat(stmt=stmt, setup=setup, repeat=rep, number=number) res_list[name[1]] = sum(results) / rep res_sort = sorted(res_list.items(), key = lambda item : item[1]) for item in res_sort: rel = item[1] / res_sort[0][1] print ('function: ' + item[0] + ', av. time sec: %9.5f, ' % item[1] + 'relative: %6.1f' % rel)
定義執(zhí)行的算法如下
from math import * def f(x): return abs(cos(x)) ** 0.5 + sin(2 + 3 * x)
對應的數(shù)學公式是
生成數(shù)據(jù)如下
i=500000 a_py = range(i)
第一個實現(xiàn)f1是在內(nèi)部循環(huán)執(zhí)行f函數(shù),然后將每次的計算結果添加到列表中,實現(xiàn)如下
def f1(a): res = [] for x in a: res.append(f(x)) return res
當然實現(xiàn)這種方案的方法不止一種,可以使用迭代器或eval函數(shù),我自己加入了使用生成器和map方法的測試,發(fā)現(xiàn)結果有明顯差距,不知道是否科學:
迭代器實現(xiàn)
def f2(a): return [f(x) for x in a]
eval實現(xiàn)
def f3(a): ex = 'abs(cos(x)) **0.5+ sin(2 + 3 * x)' return [eval(ex) for x in a]
生成器實現(xiàn)
def f7(a): return (f(x) for x in a)
map實現(xiàn)
def f8(a): return map(f, a)
接下來是使用numpy的narray結構的幾種實現(xiàn)
import numpy as np a_np = np.arange(i) def f4(a): return (np.abs(np.cos(a)) ** 0.5 + np.sin(2 + 3 * a)) import numexpr as ne def f5(a): ex = 'abs(cos(a)) ** 0.5 + sin( 2 + 3 * a)' ne.set_num_threads(1) return ne.evaluate(ex) def f6(a): ex = 'abs(cos(a)) ** 0.5 + sin(2 + 3 * a)' ne.set_num_threads(2) return ne.evaluate(ex)
上面的f5和f6只是使用的處理器個數(shù)不同,可以根據(jù)自己電腦cpu的數(shù)目進行修改,也不是越大越好
下面進行測試
func_list = ['f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8'] data_list = ['a_py', 'a_py', 'a_py', 'a_np', 'a_np', 'a_np', 'a_py', 'a_py'] perf_comp_data(func_list, data_list)
測試結果如下
function: f8, av. time sec: 0.00000, relative: 1.0 function: f7, av. time sec: 0.00001, relative: 1.7 function: f6, av. time sec: 0.03787, relative: 11982.7 function: f5, av. time sec: 0.05838, relative: 18472.4 function: f4, av. time sec: 0.09711, relative: 30726.8 function: f2, av. time sec: 0.82343, relative: 260537.0 function: f1, av. time sec: 0.92557, relative: 292855.2 function: f3, av. time sec: 32.80889, relative: 10380938.6
發(fā)現(xiàn)f8的時間最短,調(diào)大一下時間精度再測一次
function: f8, av. time sec: 0.000002483, relative: 1.0 function: f7, av. time sec: 0.000004741, relative: 1.9 function: f5, av. time sec: 0.028068110, relative: 11303.0 function: f6, av. time sec: 0.031389788, relative: 12640.6 function: f4, av. time sec: 0.053619114, relative: 21592.4 function: f1, av. time sec: 0.852619225, relative: 343348.7 function: f2, av. time sec: 1.009691877, relative: 406601.7 function: f3, av. time sec: 26.035869787, relative: 10484613.6
發(fā)現(xiàn)使用map的性能最高,生成器次之,其他方法的性能就差的很遠了。但是使用narray數(shù)據(jù)的在一個數(shù)量級,使用python的list數(shù)據(jù)又在一個數(shù)量級。生成器的原理是并沒有生成一個完整的列表,而是在內(nèi)部維護一個next函數(shù),通過一邊循環(huán)迭代一遍生成下個元素的方法的實現(xiàn)的,所以他既不用在執(zhí)行時遍歷整個循環(huán),也不用分配整個空間,它花費的時間和空間跟列表的大小是沒有關系的,map與之類似,而其他實現(xiàn)都是跟列表大小有關系的。
內(nèi)存布局
numpy的ndarray構造函數(shù)形式為
np.zeros(shape, dtype=float, order='C')
np.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
shape或object定義了數(shù)組的大小或是引用了另一個一個數(shù)組
dtype用于定于元素的數(shù)據(jù)類型,可以是int8,int32,float8,float64等等
order定義了元素在內(nèi)存中的存儲順序,c表示行優(yōu)先,F(xiàn)表示列優(yōu)先
下面來比較一下內(nèi)存布局在數(shù)組很大時的差異,先構造同樣的的基于C和基于F的數(shù)組,代碼如下:
x = np.random.standard_normal(( 3, 1500000)) c = np.array(x, order='C') f = np.array(x, order='F')
下面來測試性能
%timeit c.sum(axis=0) %timeit c.std(axis=0) %timeit f.sum(axis=0) %timeit f.std(axis=0) %timeit c.sum(axis=1) %timeit c.std(axis=1) %timeit f.sum(axis=1) %timeit f.std(axis=1)
輸出如下
loops, best of 3: 12.1 ms per loop loops, best of 3: 83.3 ms per loop loops, best of 3: 70.2 ms per loop loop, best of 3: 235 ms per loop loops, best of 3: 7.11 ms per loop loops, best of 3: 37.2 ms per loop loops, best of 3: 54.7 ms per loop loops, best of 3: 193 ms per loop
可知,C內(nèi)存布局要優(yōu)于F內(nèi)存布局
并行計算
未完,待續(xù)。。。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關文章
Python reshape的用法及多個二維數(shù)組合并為三維數(shù)組的實例
今天小編就為大家分享一篇Python reshape的用法及多個二維數(shù)組合并為三維數(shù)組的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python?paddleocr快速使用及參數(shù)配置詳解
PaddleOCR是基于PaddlePaddle深度學習框架的開源OCR工具,但它提供了推理模型/訓練模型/預訓練模型,用戶可以直接使用推理模型進行識別,也可以對訓練模型或預訓練模型進行再訓練,這篇文章主要介紹了Python?paddleocr快速使用及參數(shù)詳解,需要的朋友可以參考下2024-06-06解讀python基于netconf協(xié)議獲取網(wǎng)元的數(shù)據(jù)
大多數(shù)企業(yè)都需要網(wǎng)絡支撐企業(yè)的ICT運行,針對企業(yè)網(wǎng)絡中的網(wǎng)元設備(包括交換機,路由器,防火墻等),很多企業(yè)希望根據(jù)自身的業(yè)務特點定制網(wǎng)絡管理,如下就以華為的NE40E網(wǎng)元為例,說明如何通過python基于netconf協(xié)議實現(xiàn)對于網(wǎng)元配置數(shù)據(jù)的獲取。2021-05-05Python?生成多行重復數(shù)據(jù)的方法實現(xiàn)
本文主要介紹了Python?生成多行重復數(shù)據(jù)的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03