I have the following for-loop to construct 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-loop, especially for large values of 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 the creation of the array even further, I thought I would try to use 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 am not seeing any performance gains from using Numba which is almost 3x slower than the vectorized approach using just Numpy.
Any suggestions on how to use Numba for this?
Numba doesn't make arbitrary method calls faster. If you are calling out to a library, numba really can't do anything with that most of the time. But if you re-write things a little differently, you can still get a decent speedup (I'm using numba 0.14.0 -- if you are using a different version, hardware, etc, you might 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 about numerical precision.
The key with numba is to unroll loops and only make "primitive" arithmetic calls that numba supports
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments