I'm trying to compare numba and pure python using the basic example and I'm getting odd results.
This is the numba example:
from numba import jit
from numpy import arange
from time import time
# jit decorator tells Numba to compile this function.
# The argument types will be inferred by Numba when function is called.
@jit
def sum2d(arr):
M, N = arr.shape
result = 0.0
for i in range(M):
for j in range(N):
result += arr[i,j]
return result
a = arange(9).reshape(3,3)
t = time()
print(sum2d(a))
print time() - t
This is the timing I'm getting with numba 0.0469660758972 seconds
And without numba I'm getting a faster result 9.60826873779e-05 seconds
numba needs to compile your function based on the types of the arguments, you can either do that when defining the function by providing a signature ( eager compilation ) or you can let numba infer the types for you when you call the function for the first time (it's called just-in-time [JIT] compilation after all :-)).
You haven't specified any signature so it will infer and compile the function when you first call it. They even state that in the example you used:
# jit decorator tells Numba to compile this function.
# The argument types will be inferred by Numba when function is called.
However subsequent runs (with the same types and dtypes) will be fast:
t = time()
print(sum2d(a)) # 0.035051584243774414
print(time() - t)
%timeit sum2d(a) # 1000000 loops, best of 3: 1.57 µs per loop
The last command used IPythons
%timeit
command .
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.