I have two sets of functions with the same functionality: 1 programmed with loop, and the second with numpy operations. "Loop" is slower than numpy implementaiton.
Then I try to speed them both up with numba. The following happens: "loop"+numba becomes 100 times faster than simply "loop" and even faster than numba+numpy.
question: I would expect that numba+numpy should be at least as fast as numba+"loop"? is my assumption wrong or it is a problem with the way I implemented function with numpy? Any suggestions?
With numba+loop:
@jit()
def rebmun_of_ps_egnahcs_loop_numba(dip):
rebmun_of_ps_egnahcs = 0
for i in range(0, dip.shape[0] - 1):
if (
(dip[i + 1, 0] != dip[i, 0])
& ~np.isnan(dip[i, 0])
& ~np.isnan(dip[i + 1, 0])
& (dip[i + 1, 4] == 1)
& (dip[i, 4] == 1)
& (dip[i + 1, 3] == 0)
& (dip[i, 3] == 0)
):
rebmun_of_ps_egnahcs += 1
return rebmun_of_ps_egnahcs
@jit()
def rebmun_of_ps_egnahcs_loop_N_numba(dips_array_of_arrays):
dip_ps_egnahc_stluser = np.empty(dips_array_of_arrays.shape[0], dtype=np.float64)
dip_ps_egnahc_stluser.fill(np.nan)
for i in np.ndindex(dips_array_of_arrays.shape[0]):
dip_ps_egnahc_stluser[i] = rebmun_of_ps_egnahcs_loop_numba(dips_array_of_arrays[i])
return dip_ps_egnahc_stluser
With numba+numpy:
@jit()
def rebmun_of_ps_egnahcs_numpy_numba(dip):
rebmun_of_ps_egnahcs = 0
spt = dip[:, 0]
ma = dip[:, 4]
lr = dip[:, 3]
rebmun_of_ps_egnahcs = np.sum(
np.not_equal(spt, np.roll(spt, 1))
& ~np.isnan(spt)
& ~np.isnan(np.roll(spt, 1))
& np.equal(np.roll(ma, 1), 1) & np.equal(ma, 1)
& np.equal(np.roll(lr, 1), 0) & np.equal(lr, 0)
)
return rebmun_of_ps_egnahcs
@jit()
def rebmun_of_ps_egnahcs_numpy_N_numba(dips_array_of_arrays):
dip_ps_egnahc_stluser = np.empty(dips_array_of_arrays.shape[0], dtype=np.float64)
dip_ps_egnahc_stluser.fill(np.nan)
for i in np.ndindex(dips_array_of_arrays.shape[0]):
dip_ps_egnahc_stluser[i] = rebmun_of_ps_egnahcs_numpy_numba(dips_array_of_arrays[i])
return dip_ps_egnahc_stluser