Numba is an open-source just-in-time (JIT) compiler for Python that translates Python functions and NumPy array operations into optimized machine code using the LLVM compiler. Sponsored by Anaconda, Numba specializes in numerical and scientific computing where performance is critical, offering substantial speedups (often 10β100Γ) with minimal code changes. The key insight is that Numba targets array-oriented numerical code particularly well β loops over NumPy arrays, mathematical computations, and parallel operations that would be slow in pure Python execute at speeds approaching C or Fortran. As of Numba 0.65.x (2026), @jit defaults to nopython mode, the built-in CUDA target is deprecated in favor of the numba-cuda package, and experimental free-threading (Python 3.14t) support has been added.
What This Cheat Sheet Covers
This topic spans 20 focused tables and 191 indexed concepts, 121 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Core JIT Decorators
The decorator you choose is the most consequential decision when using Numba β it determines compilation mode, target device, and output type. Start with @njit for CPU numerical code, and reach for @vectorize or @cuda.jit only when you specifically need ufuncs or GPU execution.
| Decorator | Example | Description | |
|---|---|---|---|
def f(x): return x + 1 | β’ Alias for @jit(nopython=True) β the recommended default β’ forces nopython mode and raises error immediately if compilation fails instead of falling back. | ||
def f(x): return x * 2 | β’ Compiles using lazy JIT compilation (on first call) β’ since Numba 0.59, defaults to nopython mode (no longer falls back to object mode automatically). | ||
def compute(a, b): return a @ b | β’ Equivalent to @njitβ’ explicit form still widely seen in older code β’ Redundant since 0.59 but still valid and readable | ||
def add(x, y): return x + y | β’ Eager compilation with explicit type signature β’ compiles immediately at definition time rather than on first call. | ||
def square(x): return x ** 2 | β’ Creates a NumPy universal function (ufunc) from a scalar function β’ automatically broadcasts over arrays element-wise; supports cpu, parallel, and cuda targets. | ||
([(int64[:], int64, int64[:])], '(n),()->(n)') def g(x, y, res): for i in range(x.shape[0]): res[i] = x[i] + y | β’ Creates a generalized ufunc operating on array segments β’ layout signature specifies input/output dimensions; supports writable_args parameter for in-place modification. | ||
def kernel(arr): i = cuda.grid(1) if i < arr.size: arr[i] *= 2 | β’ Compiles function as a CUDA GPU kernel (deprecated built-in; install numba-cuda package)β’ launched with kernel[blocks, threads](args). | ||
def callback(x): return x * 2 | β’ Creates a C-callable function for use as a callback in foreign C/C++ libraries β’ .address gives raw function pointer; .ctypes gives ctypes-compatible wrapper. | ||
def smooth(a): return (a[-1] + a[0] + a[1]) / 3 | β’ Defines a neighborhood computation where each output element depends on nearby input elements β’ automatically determines border handling; combinable with parallel=True. | ||
from numba.experimental import jitclass spec = [('value', int64)] class Counter: def __init__(self, v): self.value = v | β’ Compiles an entire Python class with all methods in nopython mode β’ requires type spec for fields; now supports static methods and many dunder methods; import from numba.experimental. |