I am trying to time a simple matrix multiplication program using numpy and Numba. Here is my code
The error I am getting is get_ipython().magic('time _ = A.dot(B)') SyntaxError: invalid syntax
import time
import numpy as np
from numba import jit, njit, vectorized
A = np.random.randint(0, 15, size=(1000,1000))
B = np.random.randint(0, 15, size=(1000,1000))
%time A.dot(B)
@jit(nopython=True)
%time _ = A.dot(B) ```
Any help would be greatly appreciated!
CodePudding user response:
I think are a few misconceptions you are making, and resolving those will make your code work as well.
- The
%time
is a line magic from IPython.
This does not require a import of thetime
python package. - Decorators (like your
@jit(nopython=True)
expression) are there to wrap function or class definitions. Putting them in front of regular lines of code would give you the same error.
What you can do to make this code workable in your situation is to wrap the line of code you are trying to process in a function.
A working version of your code would be something like this:
import numpy as np
from numba import jit
A = np.random.randint(0, 15, size=(1000,1000))
B = np.random.randint(0, 15, size=(1000,1000))
%time A.dot(B)
@jit(nopython=True)
def wrapped_dot(A, B):
return A.dot(B)
%time _ = wrapped_dot(A, B)
Though I would also remark that since you are already working with numpy arrays, numba is probably not going to achieve a noticeable speedup, since numpy is already optimized.