Numba autojit function is slower than vector Numpy method

I have the following for-loop to build a list of values:

p = 7
A = []

for i in range(0, 10**p):
    A.append(i**3 + i**2)

      

To speed up the creation of the list, I created it as a Numpy array using a vectorized approach. This approach is much faster than the equivalent for a loop, especially for large values p

, which increases the range.

import numpy as np
from numba import autojit

p = 7
m = np.arange(0, 10**p)
D = np.empty(len(m))
D = m**3 + m**2

      

To speed up array creation even more, I thought I'd try using the Numba package. Below is my current attempt.

@autojit
def func(a):
    a = np.asarray(a)
    n = np.arange(0, 10**p)
    a = np.append(a, n**3 + n**2)
    return a

e = []
E = func(e)

      

Unfortunately, I see no performance gain from using Numba, which is almost 3x slower than the vector approach using Numpy alone.

Any suggestions for using Numba for this?

+3


source to share


1 answer


Numba doesn't make arbitrary method calls any faster. If you call the library, numba really can't do anything about it most of the time. But if you rewrite things a little differently, you can still get a decent speedup (I am using numba 0.14.0 - if you are using a different version, hardware, etc., you can get different results, especially since numba is in active development):

import numpy as np
import numba as nb

def func(a, p):
    a = np.asarray(a)
    n = np.arange(0, 10**p)
    a = np.append(a, n**3 + n**2)
    return a

@nb.jit
def func2(a, p):
    a = np.asarray(a)
    n = np.empty(10**p, dtype=np.float64)
    for k in range(10**p):
        n[k] = k*k*(k + 1)

    return np.append(a, n)

p = 6
e = []
E = func(e, p)
E2 = func2(e, p)
print np.allclose(E, E2)

      

And timings:



In [51]:

%timeit func(e, p)
10 loops, best of 3: 42.9 ms per loop
In [52]:

%timeit func2(e, p)
100 loops, best of 3: 3.09 ms per loop

      

Also with p=7

you need to be a little careful with numerical precision.

The key with numba is to unroll the loops and only make the "primitive" arithmetic calls that numba supports

+5


source







All Articles