Compiler Modes in Python
Overview
Time: 10 min
Learn the difference between Nopython and Object compilation.
Learn how to use Nopython compilation.
Numba offers two compilation modes: nopython mode and object mode.
Nopython Mode: Generates code that avoids using the Python C API, resulting in the highest
performance.
Explanation
Python C API allows :
Interfacing with Python objects and types.
Accessing and manipulating Python objects in C.
Embedding and extending Python with C/C++ code.
Object Mode: Handles values as Python objects and relies on the Python C API, often leading
to performance similar to standard Python code.
Explanation
Numba Python C API under the hood to interact with regular Python objects (like lists, dictionaries, or custom classes).
By default, Numba will fall back to object mode if nopython mode cannot be used. However, it might be preferable to generate an error rather than automatically falling back to object mode.
1@jit(nopython=True)
2def with_numba(a):
3 trace = 0.0
4 for i in range(a.shape[0]):
5 trace += np.tanh(a[i, i])
6 return a + trace
Setting nopython=True will trigger an error if nopython mode fails to apply. You can also use @jit(nopython=True) interchangeably with @njit.
1@njit
2def with_numba(a):
3 trace = 0.0
4 for i in range(a.shape[0]):
5 trace += np.tanh(a[i, i])
6 return a + trace
Key Points
nopythonmode gives the best performance.There is no fallback option in the
nopythonmode.