Numba: High Performance Python Compiler
Numba is an open-source, NumPy-aware optimizing Just-In-Time (JIT) compiler for Python. It translates a subset of Python and NumPy code into fast machine code using the LLVM compiler library, enabling numerical algorithms to approach the speeds of C or Fortran without requiring a separate compilation step. Numba is currently at version 0.64.0 and maintains a regular release cadence, often coinciding with Python and NumPy releases.
Warnings
- gotcha Numba treats global variables as compile-time constants. Modifying a global variable after a `@jit` decorated function has been compiled will not affect the compiled function's behavior unless the function is explicitly recompiled using `.recompile()` or the global is passed as an argument.
- gotcha Type inference failures are a common reason for Numba compilation errors, especially in `nopython` mode. Numba needs to determine concrete types for all variables; if it cannot, compilation will fail.
- gotcha Running Numba-compiled scripts twice in IDEs like Spyder can lead to `TypeError: No matching definition for argument type(s)` due to module reloading issues.
- deprecated The `target` kwarg for the `numba.jit` decorator family has been deprecated.
- deprecated Reflection for Python `List` and `Set` types within Numba-compiled code is deprecated. This feature ensured changes to mutable Python containers were visible after the function returned.
- breaking Numba 0.64.0 supports NumPy 2.3 and 2.4. NumPy 2.0 introduces binary incompatible changes and a new type system. Numba has evolved to accommodate this, but users should be prepared to test and verify their codebases, as NumPy 2.0 may also impact non-JIT compiled code output.
- breaking The experimental RVSDG (Region-based Value Stream Dependence Graph) frontend was removed in Numba 0.61.0.
Install
-
pip install numba -
conda install numba
Imports
- jit
from numba import jit
- njit
from numba import njit
- guvectorize
from numba import guvectorize
Quickstart
import numpy as np
from numba import njit
@njit
def sum_array(arr):
total = 0.0
for x in arr:
total += x
return total
# Example usage
data = np.arange(1000000, dtype=np.float64)
result = sum_array(data)
print(f"Sum of array: {result}")