I was experimenting with Numba's @jit
and @guvectorize
and find that @guvectorize
is considerably slower than @jit
. For example, I have the following code that calculates the rolling moving average:
import numpy as np
from numba import *
@guvectorize(['void(float64[:], float64[:], float64[:])'], '(n),()->(n)')
def sma(x, m, y):
n = x.shape[0]
mi = int(m)
y[:] *= np.nan
for i in range(mi-1, n):
for j in range(i-mi+1, i+1):
y[i] = x[j] if j == i-m+1 else y[i]+x[j]
y[i] /= double(mi)
@jit(float64[:](float64[:], float64))
def sma1(x, m):
n = x.shape[0]
mi = int(m)
y = np.empty(x.shape[0]) * np.nan
for i in range(mi-1, n):
for j in range(i-mi+1, i+1):
y[i] = x[j] if j == i-m+1 else y[i]+x[j]
y[i] /= double(mi)
return y
Here is the testing code:
import movavg_nb as mv1
import numpy as np
x = np.random.random(100)
import time as t
t0 = t.clock()
for i in range(10000):
y = mv1.sma(x, 5)
print(t.clock()-t0)
t0 = t.clock()
for i in range(10000):
y = mv1.sma1(x, 5)
print(t.clock()-t0)
I ran this twice, because Numba usually needs to assign types the first time. Here are the results of the testing code for the second time:
17.459737999999998 # corresponding to @guvectorize
0.036977999999997735 # corresponding to @jit
The order of magnitude is > 450x
Question: I can understand the purpose of @vectorize
(where the inputs are the same), but what would be the purpose of @guvectorize
when @jit
is faster? (or it there something in my code that is slowing it?)