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.
What This Cheat Sheet Covers
This topic spans 20 focused tables and 178 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Core JIT Decorators
| Decorator | Example | Description |
|---|---|---|
def f(x): return x * 2 | • Compiles functions using lazy JIT compilation (code compiled on first call) • attempts nopython mode first, falls back to object mode if needed. | |
def f(x): return x + 1 | • Alias for @jit(nopython=True) • forces nopython mode and raises error if compilation fails instead of falling back. | |
def compute(a, b): return a @ b | • Explicitly requires nopython mode compilation (no Python C API calls) • produces fastest code but has stricter limitations. | |
def add(x, y): return x + y | • Eager compilation with explicit type signature • compiles immediately when function is defined rather than on first call. | |
def square(x): return x ** 2 | • Creates NumPy universal function (ufunc) from scalar function • automatically broadcasts over arrays with element-wise operations. |