This function takes two inputs: A
is 2D (N,5) while B
is 1D (N).
It tries to find the smallest j
such that A[i,j] > B[i]
for all i
.
Using numpy with argmax trick is slower than this version where I loop manually through all i
and compile it with numba. I think the reason why is because as N
gets much bigger (~60000), it's more expensive to index on the first dimension, rather than to just loop and hoping to stop at small j
, e.g., j=2 or j=3
.
Any ideas how to further speed things up?
@nb.jit(nb.int64[:](nb.float32[:,:],nb.float32[:]),nopython=True,cache=True)
def find_passed_hz(A, B):
C = np.empty(len(B), dtype=np.int64)
_, m = A.shape # m=5
n = len(current_times)
for i in range(n):
for j in range(m):
if A[i,j] > B[i]:
C[i] = j
break
return C
NOTE: every row of A
is sorted in ascending order, but because m=5
very small for numpy search_sorted or binary search to have any effect (or not? I tried but it seems to have no effect).
j
such that… is such aj
guaranteed to exist? \$\endgroup\$j
such thatA[i,j] > B[i]
for alli
is not what I see the code do: return a NumPy arrayC
of smallest elements ofA[i,j]
exceedingB[i]
for eachi
. \$\endgroup\$j
guaranteed to exist, because I setA[i,4]
very big. for the second comment, yes, that's what I meant. can you suggest the better wordings to be more specific? or I simply insert your second comment in the question? Thank you. \$\endgroup\$