The Numba software performs just-in-time compiling using special function decorators. The compilation produces native machine code automatically. The generated code can run on CPUs and GPUs. The main use case for Numba is math-heavy code that uses NumPy arrays.
We can compile the code with the @numba.jit
decorator with optional function signature (for instance, int32(int32)
). The types correspond with similar NumPy types. Numba operates in the nopython
and object
modes. The nopython
mode is faster but more restricted. We can also release the Global Interpreter Lock (GIL) with the nogil
option. You can cache the compilation results by requesting a file cache with the cache
argument.
The @vectorize
decorator converts functions with scalar arguments into NumPy ufuncs. Vectorization gives extra advantages, such as automatic broadcasting, and can be used on a single core, multiple cores in parallel, or a GPU.