导入必备包importnumpyasnpimportpandasaspdimporttimefromnumpy。testingimportfromscipy。linalgimportimportgcfrommatplotlibimportpyplotaspltmatplotlibinlinefromnumbaimportjit,void,doublePythondefdetbylu(y,x):y〔0〕1。Nx。shape〔0〕forkinrange(N):y〔0〕x〔k,k〕foriinrange(k1,N):x〔i,k〕x〔k,k〕forjinrange(k1,N):x〔i,j〕x〔i,k〕x〔k,j〕defrunpython(A,B,y,N):checkthatresultiscorrectnp。copyto(B,A)detbylu(y,B)Lnp。tril(B,1)np。eye(N)Unp。triu(B)assertalmostequal(L。dot(U),A)gc。disable()sttime。time()loops1(100000(NN))forlinrange(loops):np。copyto(B,A)detbylu(y,B)ettime。time()gc。enable()return(etst)loopsNumpydefnumpydetbylu(y,x):y〔0〕1。Nx。shape〔0〕withnp。errstate(invalidignore):forkinrange(N):y〔0〕x〔k,k〕xkx〔k〕foriinrange(k1,N):xix〔i〕xi〔k〕xk〔k〕xi〔k1:〕xi〔k〕xk〔k1:〕defrunnumpy(A,B,y,N):checkthatresultiscorrectnp。copyto(B,A)numpydetbylu(y,B)Lnp。tril(B,1)np。eye(N)Unp。triu(B)assertalmostequal(L。dot(U),A)gc。disable()sttime。time()loops1(100000(NN))forlinrange(loops):np。copyto(B,A)numpydetbylu(y,B)ettime。time()gc。enable()return(etst)loopsNumbadefnumbadetbylu(y,x):y〔0〕1。Nx。shape〔0〕forkinrange(N):y〔0〕x〔k,k〕foriinrange(k1,N):x〔i,k〕x〔k,k〕forjinrange(k1,N):x〔i,j〕x〔i,k〕x〔k,j〕fastdetbylujit(void(double〔:〕,double〔:,:〕))(numbadetbylu)defrunnumba(A,B,y,N):checkthatresultiscorrectnp。copyto(B,A)fastdetbylu(y,B)Lnp。tril(B,1)np。eye(N)Unp。triu(B)assertalmostequal(L。dot(U),A)gc。disable()sttime。time()loops1min(1000000(NN),20000)forlinrange(loops):np。copyto(B,A)fastdetbylu(y,B)ettime。time()gc。enable()return(etst)loopsCythonloadextcythoncythonimportcythoncython。boundscheck(False)cython。wraparound(False)cpdefcythondetbylu(double〔:〕y,double〔:,:〕x):y〔0〕1。cdefintNx。shape〔0〕cdefinti,j,kforkinrange(N):y〔0〕x〔k,k〕foriinrange(k1,N):x〔i,k〕x〔k,k〕forjinrange(k1,N):x〔i,j〕x〔i,k〕x〔k,j〕defruncython(A,B,y,N):checkthatresultiscorrectnp。copyto(B,A)cythondetbylu(y,B)Lnp。tril(B,1)np。eye(N)Unp。triu(B)assertalmostequal(L。dot(U),A)gc。disable()sttime。time()loops1min(1000000(NN),20000)forlinrange(loops):np。copyto(B,A)cythondetbylu(y,B)ettime。time()gc。enable()return(etst)loopsCfromcffiimportFFIffiFFI()ffi。cdef(voiddetbylu(doubley,doubleB,intN);)Cffi。dlopen(rC:UsersIBMADMINlu。dll)cdetbyluC。detbyludefrunc(A,B,y,N):runccodeBnumpy。zeros((N,N),orderF)B〔:,:〕Anp。copyto(B,A)cdetbylu(ffi。cast(double,y。ctypes。data),ffi。cast(double,B。ctypes。data),ffi。cast(int,N))checkthatresultiscorrectLnp。tril(B,1)np。eye(N)Unp。triu(B)assertalmostequal(L。dot(U),A)gc。disable()sttime。time()loops1min(1000000(NN),20000)forlinrange(loops):np。copyto(B,A)cdetbylu(ffi。cast(double,y。ctypes。data),ffi。cast(double,B。ctypes。data),ffi。cast(int,N))ettime。time()gc。enable()return(etst)loopsScipydefrunscipy(A,B,y,N):checkthatresultiscorrectnp。copyto(B,A)(P,L,U)lu(B,overwriteaTrue)assertalmostequal(P。dot(L。dot(U)),A)gc。disable()sttime。time()loops1min(1000000(NN),20000)forlinrange(loops):np。copyto(B,A)lu(B,overwriteaTrue)ettime。time()gc。enable()return(etst)loopsLapackdefrunlapack(A,B,y,N):checkthatresultiscorrectgc。disable()sttime。time()loops1min(1000000(NN),20000)forlinrange(loops):np。copyto(B,A)lufactor(B,overwriteaTrue)ettime。time()gc。enable()return(etst)loopsTimingsdeftimings(n7,series〔purepython,c,numba,numpy,cython,scipy,lapack,julia〕):Nsnp。array(〔5,10,30,100,200,300,400,600,1000,2000,4000,8000〕)Fs〔runpython,runc,runnumba,runnumpy,runcython,runscipy,runlapack〕timespd。DataFrame(np。zeros((n,len(Fs)1)),indexNs〔:n〕,columnsseries)fori,Ninenumerate(Ns〔:n〕):print(N,N,end)Anp。random。random((N,N))Bnp。empty(A。shape)ynp。zeros(1)forj,labelinenumerate(series〔:1〕):iflabel!:print(j,end)times。loc〔N,label〕Fs〔j〕(A,B,y,N)print()returntimes执行,统计耗时timestimings(9)N50123456N100123456N300123456N1000123456N2000123456N3000123456N4000123456N6000123456N10000123456结果times python c numba numpy cython scipy lapack julia 5hr0。000051 0。000016 0。000002 0。000074 0。000006 0。000029 0。000031 6。091400e07 10hr0。000312 0。000016 0。000003 0。000234 0。000006 0。000030 0。000031 1。060710e06 30hr0。007800 0。000028 0。000014 0。001950 0。000014 0。000070 0。000056 9。082080e06 100hr0。289310 0。000154 0。000463 0。029782 0。000309 0。000309 0。000309 2。265530e04 200hr2。277604 0。001800 0。007200 0。119600 0。003600 0。001200 0。001200 1。740604e03 300hr7。636214 0。007800 0。019500 0。226200 0。007800 0。003900 0。001300 5。823171e03 400hr18。267632 0。017829 0。051257 0。514801 0。020057 0。008914 0。002229 1。372135e02 600hr62。197309 0。062400 0。124800 0。982802 0。088400 0。036400 0。010400 4。543215e02 1000hr290。472510 0。257401 0。569401 3。042005 0。288600 0。070200 0。039000 2。642414e01 画图defplottimes(times,cols〔〕,nameruntimes。png):plt。figure(figsize(7,5))ifcols〔〕:colstimes。columnsfori,labelinenumerate(cols):iflabel!:plt。loglog(times。index,times〔label〕,labellabel)plt。xlabel(N(matrixsize))plt。ylabel(runtime〔sec〕)plt。grid()plt。legend(loc2)plt。savefig(name)plt。show()plottimes(times,cols〔purepython,c,numba〕,nameruntimes1) plottimes(times,cols〔purepython,c,cython〕,nameruntimes2) plottimes(times,cols〔purepython,c,numpy〕,nameruntimes3) plottimes(times,cols〔purepython,c,scipy,lapack〕,nameruntimes4) plottimes(times,cols〔purepython,c,julia〕,nameruntimes5)