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!
I think are a few misconceptions you are making, and resolving those will make your code work as well.
%time
is a line magic from IPython. time
python package.@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.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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.