在这里使用np.min
有什么问题?为什么numba不喜欢在那个函数中使用列表,有没有其他方法可以让np.min
工作?在
from numba import njit
import numpy as np
@njit
def availarray(length):
out=np.ones(14)
if length>0:
out[0:np.min([int(length),14])]=0
return out
availarray(3)
该函数与min
配合使用很好,但是np.min
应该更快。。。在
Tags:
问题是
np.min
的numba版本需要一个array
作为输入。在更好的解决方案是只使用numba版的Python
^{pr2}$min
:因为}实际上都是这些函数的Numba版本(至少在
np.min
和{njit
ted函数中)min
在这种情况下也应该快得多。但是这不太可能引起注意,因为数组的分配和将一些元素设置为零将是这里主要的运行时贡献者。在请注意,这里甚至不需要
min
调用,因为即使使用更大的停止索引,切片也会隐式地停止在数组的末尾:要使您的代码与
numba
一起工作,您必须将np.min
应用于NumPy数组,这意味着您必须将列表[int(length),14]
转换为NumPy数组,如下所示相关问题 更多 >
编程相关推荐