在以下代码中,test_func_1比test_func_2慢一个数量级。是否无法改善或甚至匹配此操作的numpy性能?
from numba import guvectorize
import numpy as np
@guvectorize(['void(float64[:,:,:], float64[:], float64[:,:,:])'], '(n,o,p),(n)->(n,o,p)', nopython=True)
def test_func_1(time_series, areas, res):
for i in range(areas.size):
area = areas[i]
adjusted_area = (area / 10000.) ** .12 # used to adjust erosion
for k in range(time_series.shape[0]):
res[i, 0, k] = time_series[i, 0, k] * area
res[i, 1, k] = time_series[i, 1, k] * adjusted_area
res[i, 2, k] = time_series[i, 2, k] * area
res[i, 3, k] = time_series[i, 3, k] * adjusted_area
def test_func_2(time_series, areas):
array = np.swapaxes(time_series, 0, 2)
array[:, :2] *= areas
array[:, 2:] *= (areas / 10000.) ** .12
return array
dummy = np.float32(np.random.randint(0, 10, (20, 5, 5000)))
areas = np.float32(np.random.randint(0, 10, 20))
test_func_1(dummy, areas)
test_func_2(dummy, areas)
答案 0 :(得分:4)
正如@JoshAdel在评论中所指出的,这里的关键区别在于你的numba
版本正在分配并填充一个新数组,而numpy正在修改原始数据。
为numpy添加一个合适的.copy()
会让它稍慢一些。你也可以让你的numba版本正常工作 - 据我所知,使用gufunc是不可能的,但如果你不需要gufunc提供的广播,那么你将使用常规的jit函数。
def test_func_2(time_series, areas):
array = np.swapaxes(time_series, 0, 2).copy()
array[:, :2] *= areas
array[:, 2:] *= (areas / 10000.) ** .12
return array
dummy = np.float32(np.random.randint(0, 10, (20, 5, 5000)))
areas = np.float32(np.random.randint(0, 10, 20))
%timeit test_func_1(dummy, areas)
1.21 ms ± 5.33 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit test_func_2(dummy, areas)
1.77 ms ± 15.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)