Setup
I have the following two implementations of a matrix-calculation:
- The first implementation uses a
matrix of shape (n, m)
and the calculation is repeated in a for-loop forrepetition
-times:
import numpy as np
from numba import jit
@jit
def foo():
for i in range(1, n):
for j in range(1, m):
_deleteA = (
matrix[i, j] +
#some constants added here
)
_deleteB = (
matrix[i, j-1] +
#some constants added here
)
matrix[i, j] = min(_deleteA, _deleteB)
return matrix
repetition = 3
for x in range(repetition):
foo()
2. The second implementation avoids the extra for-loop and, hence, includes repetition = 3
into the matrix, which is then of shape (repetition, n, m)
:
@jit
def foo():
for i in range(1, n):
for j in range(1, m):
_deleteA = (
matrix[:, i, j] +
#some constants added here
)
_deleteB = (
matrix[:, i, j-1] +
#some constants added here
)
matrix[:, i, j] = np.amin(np.stack((_deleteA, _deleteB), axis=1), axis=1)
return matrix
Questions
Regarding both implementations, I discovered two things regarding their performance with %timeit
in iPython.
- The first implementation profits hugely from
@jit
, while the second does not at all (28ms vs. 25sec in my testcase). Can anybody imagine why@jit
does not work anymore with a numpy-array of shape(repetition, n, m)
?
Edit
I moved the former second question to an extra post since asking multiple questions is concidered bad SO-style.
The question was:
- When neglecting
@jit
, the first implementation is still a lot faster (same test-case: 17sec vs. 26sec). Why is numpy slower when working on three instead of two dimensions?