Numba - @jit basic usage

https://numba.readthedocs.io/en/stable/user/5minguide.html

LAZY COMPILATION

The recommended way to use the @jit decorator is to let Numba decide when and how to optimize.


from numba import jit

@jit(nopython=True)
def f(x, y):
    return x + y

print(f(1,2))   # return 3
print(f(1.3,2))   # return 3.3
print(f(1j,2))   # return (2+1j)
# Three calls will generate different code paths.

EAGER COMPILATION

We can tell Numba the function signature we are expecting. No other specialization will be allowed.


from numba import jit, int64

@jit(int64(int64, int64),nopython=True)
def f(x, y):
    return x + y

print(f(1,2))   # return 3
print(f(1.3,2))   # return 3 (float -> int)
#print(f(1j,2))   # TypeError

COMPILATION OPTIONS

A number of keyword-only arguments can be passed to the @jit decorator.


nopython=True   # force 'nopython mode'
forceobj=True   # force 'object mode'
fastmath=True   # relax some numerical rigour
parallel=True   # enables automatic parallelization (no GIL!),only in 'nopython mode'
nogil=True   # Numba will release the GIL
cache=True   # using a file-based cache
debug=True   # for debugging