Numba vs. Cython: Take 2
numbacythonpythonperformancescientific-computingjitbenchmark
Abstraction: Python performance benchmark comparing Numba JIT and Cython for pairwise distance computation
Key points:
- Benchmark task: pairwise distance on 1000 points in 3D — Python loop=13.4s, numpy broadcast=111ms, Cython=9.87ms, Numba=9.12ms
- Numba uses LLVM JIT; wrapping plain Python with
autojitdecorator achieves ~1400x speedup with no type annotations needed - Cython requires explicit type declarations and is a Python/C hybrid; despite full optimization, was ~8% slower than Numba here
- scipy
cdist(C): 12.9ms; sklearneuclidean_distances(Cython): 35.6ms; Fortran/f2py: 16.7ms — all slower than Numba - numpy broadcasting allocates large temporary arrays (MMN elements), causing memory overhead vs. loop-based approach
- PyPy excluded: does not support NumPy, making it unsuitable for scientific Python workflows
Connections: Numba · Cython · Numpy · Scipy · Scikit Learn · Python Performance Optimization · Jit Compilation · Scientific Computing
Source: http://nbviewer.ipython.org/url/jakevdp.github.io/downloads/notebooks/NumbaCython.ipynb