我一直在尝试使用numba运行代码,我还添加了一个打印以查看代码的进度:
from numba import jit,njit,prange
import numpy as np
# for minimum reproducible example
a=1e5
ar = np.random.rand(a)
at = np.random.rand(a)
an = np.random.rand(a)
###############################3
tau = 1 # time lag
window = 6000
@njit(parallel=True)
def func_DB(ar,at,an):
DBtotal= np.zeros((len(an)-tau))
k = 0
for i in prange(0,len(an)-tau,1):
DBtotal[i] = np.sqrt((ar[i + tau]- ar[i])**2 +(at[i + tau]- at[i])**2 +(an[i + tau]- an[i])**2)
## To print the progress
if i%1e5==0:
k+=1
print(k*1e5/len(DBtotal))
return DBtotal
@njit(parallel=True)
def func_PVI(tau, window):
PVI = np.zeros((len(DBtotal)))
k = 0
for i in prange(int(window/2),len(DBtotal)-int(window/2)):
PVI[i] = DBtotal[i]/np.sqrt((np.mean(DBtotal[i-int(window/2):i+int(window/2)]**2)))
# To print the progress
if i%1e5==0:
k+=1
print(k*1e5/len(DBtotal))
return PVI
DBtotal = func_DB(ar,at,an)
PVI = func_PVI(DBtotal,tau, window)
但是,当代码运行时,我没有得到我期望的结果(即,随着代码的运行,值从0变为1),而是得到以下结果:
Out[:] 0.009479390005044932
0.009479390005044932
0.009479390005044932
0.009479390005044932
0.009479390005044932
0.018958780010089864
有人能推荐一种查看代码进度的方法吗
此外,任何提高代码效率的建议都将不胜感激
我将函数分解为若干部分,并在其周围包装了一个TQM
而不是
我曾经
试试这个:
相关问题 更多 >
编程相关推荐