Using Numba to improve finite-differences laplacian
<p>I am using Python to solve a reaction-diffusion system of equations (<a href="https://en.wikipedia.org/wiki/FitzHugh%E2%80%93Nagumo_model" rel="nofollow">Fitz-Hugh-Nagumo model</a>). I would like to learn how to use Numba in order to accelerate the calculation. I am currently importing the following laplacian.py module to my integration script:</p>

<pre><code>def neumann_laplacian_1d(u,dx2):
    """Return finite difference Laplacian approximation of 2d array.
    Uses Neumann boundary conditions and a 2nd order approximation.
    """
    laplacian = np.zeros(u.shape)
    laplacian[1:-1] =  ((1.0)*u[2:] 
                       +(1.0)*u[:-2]
                       -(2.0)*u[1:-1])
    # Neumann boundary conditions
    # edges
    laplacian[0]  =  ((2.0)*u[1]-(2.0)*u[0])
    laplacian[-1] =  ((2.0)*u[-2]-(2.0)*u[-1])

    return laplacian/ dx2
</code></pre>

<p>Where <code>u</code> is a NumPy 1D array that stands for one of the fields. I tried to add the decorator <code>@autojit(target="cpu")</code> after importing <code>from numba import autojit</code>. I didn't see any improvement in the calculation. Could anyone give me a hint how to use Numba properly in this case?</p>

<p>The input array I have used here is</p>

<pre><code>a = random.random(252)
</code></pre>

<p>And so I've compared the performance with the line:</p>

<pre><code>%timeit(neumann_laplacian_1d(a,1.0))
</code></pre>

<p>With Numba I got:</p>

<pre><code>%timeit(neumann_laplacian_1d(a,1.0))
The slowest run took 22071.00 times longer than the fastest. This could mean that an intermediate result is being cached 
1 loops, best of 3: 14.1 µs per loop
</code></pre>

<p>Without Numba I got (!!):</p>

<pre><code>%timeit(neumann_laplacian_1d(a,1.0))
The slowest run took 11.84 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 9.12 µs per loop
</code></pre>

<p>Numba actually make it slower..</p>