Pythonic Perambulations
numbacythonpythonperformancescientific-computingjit-compilation
Abstraction: Benchmark comparison of Numba JIT versus Cython for Python numerical code
Key points:
- Benchmark task: pairwise distance computation on 1000 points in 3D — a common pattern in ML and statistics.
- Pure Python loop is ~100x slower than NumPy broadcasting; NumPy broadcasting uses hidden temporary arrays causing memory overhead.
- Numba
autojitdecorator (LLVM-based JIT) yields a ~1400x speedup over pure Python — about 50% faster than the previous year's Numba release. - Highly optimized Cython (with all type annotations and bounds-checking disabled) is a few percent slower than plain Numba despite requiring years of Cython expertise.
- Fortran/f2py is ~2x slower than both Numba and Cython;
scipy.spatial.distance.cdistis ~50% slower than Numba;sklearn.metrics.euclidean_distancesis several times slower. - Timings summary: Python loop 13.4s, NumPy 0.111s, sklearn 0.0356s, Fortran 0.0167s, scipy 0.0129s, Cython 0.00987s, Numba 0.00912s.
Connections: Numba · Cython · Numpy · Scikit Learn · Jit Compilation · Scientific Computing · Performance Optimization
Source: http://jakevdp.github.io/blog/2013/06/15/numba-vs-cython-take-2/