Python 使用fortran和f2py在数组中搜索比np更快的值,其中(ar==value)

Python 使用fortran和f2py在数组中搜索比np更快的值,其中(ar==value),python,numpy,search,fortran,Python,Numpy,Search,Fortran,我试图使用np.where来定位numpy数组中的一个值的索引(数百个数组也是如此)。它不是那么慢,但它是一个瓶颈,所以我开始用fortran和f2py进行实验,并编写了这个简单的例程 subroutine find_index(array1, num_elements, target_value, loc) real, intent(in) :: array1(:) integer, intent(in) :: num_elements, target_value in

我试图使用np.where来定位numpy数组中的一个值的索引(数百个数组也是如此)。它不是那么慢,但它是一个瓶颈,所以我开始用fortran和f2py进行实验,并编写了这个简单的例程

subroutine find_index(array1, num_elements, target_value, loc)
    real, intent(in) :: array1(:)
    integer, intent(in) :: num_elements, target_value
    integer, intent(out) :: loc
    do i = 1, num_elements
        if (array1(i) .eq. target_value) then
            loc = i
            exit
        endif
    end do
end subroutine
但仍然没有改进(与np.where相同)。所以我想这是关于方法的。对改进代码(python或fortran)有什么建议吗

编辑
我正在搜索的值是整数数组中的整数

自从我使用fortran和f2py以来,已经有一段时间了,但去年我用cython做了类似的事情

在匈牙利算法搜索问题中,我需要根据行和列掩蔽数组在2d数组中查找第一个0值

因此,使用
where
argwhere
只是
np.transpose(np.where(…)
),函数是:

def find_a_zero(self):
    # find first uncovered 0 cost
    rc = np.argwhere((self.cost + self.rc[:,None] + self.cc) == 0)
    if rc.shape[0]>0:
        return tuple(rc[0])
    else:
        return None, None
我通过
argmax
获得了很好的加速:

def find_a_zero(self):
    # big help time wise, 16->10 for n=200
    cond = (self.cost + self.rc[:,None] + self.cc) == 0
    if np.count_nonzero(cond):
        idx = np.unravel_index(np.argmax(cond), cond.shape)
        return idx
    return None, None
np.其中
使用
count\u nonzero
来确定其返回数组的大小。
argmax
,当对布尔值进行操作时,第一个
上的短路为True

我用这个
cython
版本获得了更好的速度:

cdef find_a_zero(int[:,:] cost, int[:] row_cover, int[:] col_cover):
    n = cost.shape[0]
    m = cost.shape[1]
    cdef size_t r, c
    for r in range(n):
        for c in range(m):
            if (cost[r,c]==0) and (row_cover[r]==0) and (col_cover[c]==0):
                row = r
                col = c
                return r, c
    return -1, -1
如果我对
cython
的内存是正确的,那么像
int[:,:]cost
这样的定义将调用其类型化的memoryview。它具有高效的低级数组操作


类似于y=minloc(abs(array1-target_值))并检查数组是否为(y)==目标值。请注意,与浮点值的比较应使用较小的增量!为什么ypu认为cslling Fortran会更快?当然numpy例程是用C或Fortran实现的,并经过了彻底优化。此外,我在回答中澄清了我正在搜索的值是整数。RRAY1仍然是real类型。请DO不要用大写字母尖叫!显示的代码中的变量
array1
声明为
real
,无论使用多大的字母。