when is it suitable to use numba?

0 views
Skip to first unread message

jiedon...@gmail.com

unread,
Dec 11, 2016, 1:39:33 PM12/11/16
to Numba Public Discussion - Public
I am new to Numba and have read some blogs about accelerating python code using Numba. I tried the example from the homepage of numba
But I find that actually, without using the @jit decorator, the script runs faster.  I tried several arrays with different sizes. In all cases, the plain
function without using numba is faster to run. 

So my question is, under what situation is using numba actually better than using the plain function.  

Stanley Seibert

unread,
Dec 11, 2016, 1:49:07 PM12/11/16
to Numba Public Discussion - Public
Numba will be a benefit for functions with the following characteristics:
  • Run time is primarily due to NumPy array element memory access or numerical operations (integer or float) more complex than a single NumPy function call.
  • Functions which work with data types that are frequently converted by NumPy functions to int64 or float64 for calculations (like int8 and int16).
  • The function is called many times during normal execution.  Compilation is slow, so if the function is not called more than once, the execution time savings is unlikely to compensate for compilation time.
  • The function execution time is larger than the Numba dispatcher overhead.  Functions which execute in much less than a microsecond are not going to see a major improvement, as the wrapper code which transitions from the Python interpreter to Numba takes longer than a pure Python function call.

Another thing to verify in your tests is whether or not the compiler was able to fully translate the function to nopython mode.  You can add the nopython=True option to the @jit decorator to raise an exception if nopython mode was not possible.


--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users+unsubscribe@continuum.io.
To post to this group, send email to numba...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/25c5dddc-83e2-4dda-ba6a-183519d5c967%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Kevin Sheppard

unread,
Dec 11, 2016, 3:58:40 PM12/11/16
to Numba Public Discussion - Public
You need to execute the example on the homepage twice to see any benefits.

from numba import jit
from numpy import arange

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

sum2d_jit = jit(sum2d)

a = arange(9).reshape(3,3)

# Warm up
sum2d(a)
sum2d_jit(a)

%timeit sum2d(a)
100000 loops, best of 3: 6.59 µs per loop

%timeit sum2d_jit(a)
1000000 loops, best of 3: 368 ns per loop

Produces a speed up of about 16 times.


On Sun, Dec 11, 2016 at 6:49 PM Stanley Seibert <stan.s...@continuum.io> wrote:
Numba will be a benefit for functions with the following characteristics:
  • Run time is primarily due to NumPy array element memory access or numerical operations (integer or float) more complex than a single NumPy function call.
  • Functions which work with data types that are frequently converted by NumPy functions to int64 or float64 for calculations (like int8 and int16).
  • The function is called many times during normal execution.  Compilation is slow, so if the function is not called more than once, the execution time savings is unlikely to compensate for compilation time.
  • The function execution time is larger than the Numba dispatcher overhead.  Functions which execute in much less than a microsecond are not going to see a major improvement, as the wrapper code which transitions from the Python interpreter to Numba takes longer than a pure Python function call.

Another thing to verify in your tests is whether or not the compiler was able to fully translate the function to nopython mode.  You can add the nopython=True option to the @jit decorator to raise an exception if nopython mode was not possible.

On Sun, Dec 11, 2016 at 9:21 AM, <jiedon...@gmail.com> wrote:
I am new to Numba and have read some blogs about accelerating python code using Numba. I tried the example from the homepage of numba
But I find that actually, without using the @jit decorator, the script runs faster.  I tried several arrays with different sizes. In all cases, the plain
function without using numba is faster to run. 

So my question is, under what situation is using numba actually better than using the plain function.  

--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.

--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.

To post to this group, send email to numba...@continuum.io.

Benjamin Kimock

unread,
Dec 11, 2016, 4:00:45 PM12/11/16
to numba...@continuum.io
Did you set nopython=True? Functions compiled in object mode are usually shower.

--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users+unsubscribe@continuum.io.
Reply all
Reply to author
Forward
0 new messages