Learn Numba

JIT compiler that turns numeric Python and NumPy code into fast machine code

14 lessons · 3 hours · 4 levels · covers Numba 0.68

Numba is a just-in-time compiler for Python that uses LLVM to compile functions working on numbers and NumPy arrays into native machine code. Adding a decorator such as @njit to a loop-heavy function often makes it run at C speed, with optional multithreading and custom ufuncs. It is used in scientific computing, simulation and data processing wherever a loop cannot be vectorized cleanly.

Before you start

Numba builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 40 min in total
2. PracticalUse it productively at work, with the usual idioms8 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs11 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know14 lessons · 3 hours in total

The Numba tree

NumbaWhat Numba does: @njitWhat Numba does:@njitCompilation, warm-up and cachingCompilation,warm-up and cachi…What nopython mode supportsWhat nopython modesupportsWriting fast loops over arraysWriting fast loopsover arraysType inference and explicit signaturesType inference andexplicit signatures@vectorize and @guvectorize ufuncs@vectorize and@guvectorize ufuncsTyped List and DictTyped List and DictNumba vs NumPy vs othersNumba vs NumPy vsothersThe ndarrayThe ndarrayData types (dtypes)Data types (dtypes)Indexing and slicingIndexing and slicingUniversal functions (ufuncs)Universal functions(ufuncs)BroadcastingBroadcasting
Essentials and Practical shown (8 of 14 lessons); the full list is below. Click a lesson to read it.

All 14 lessons, in order

Getting started

  1. What Numba does: @njit · Essentials · 9 min

    Decorate a numeric Python function with @njit and Numba compiles it to machine code the first time it is called.

  2. Compilation, warm-up and caching · Essentials · 9 min

    Understand why the first call is slow, how to benchmark correctly and how cache=True keeps compiled code on disk.

  3. Writing fast loops over arrays · Essentials · 11 min

    Write explicit index loops over NumPy arrays, preallocate outputs and iterate in memory order for Numba speed.

Types and data

  1. What nopython mode supports · Essentials · 11 min

    Know which Python features and NumPy functions compile in nopython mode, and what to do about the rest.

  2. Type inference and explicit signatures · Practical · 12 min

    See how Numba infers types, read compiled signatures and give explicit ones for eager compilation.

  3. Typed List and Dict · Practical · 10 min

    Use numba.typed.List and numba.typed.Dict for homogeneous lists and hash maps inside compiled code.

  4. Reading errors and debugging · Advanced · 12 min

    Diagnose TypingErrors, disable the JIT to debug in Python, and escape to object mode for unsupported calls.

Ufuncs and vectorizing

  1. @vectorize and @guvectorize ufuncs · Practical · 12 min

    Write scalar functions and turn them into real NumPy ufuncs with broadcasting, or generalized ufuncs that work on sub-arrays.

Parallelism and tuning

  1. Numba vs NumPy vs others · Practical · 9 min

    Decide when Numba beats vectorized NumPy, when it does not help, and what the alternatives are.

  2. Multithreading with parallel=True and prange · Advanced · 13 min

    Run loops on all cores with parallel=True and prange, and understand reductions, races and threading layers.

  3. fastmath, boundscheck and other options · Advanced · 11 min

    Use compiler options like fastmath, boundscheck, error_model and inline to trade strictness for speed knowingly.

Internals and extending

  1. Inside the compiler: from bytecode to LLVM · Expert · 14 min

    Follow Numba's pipeline from Python bytecode through Numba IR and type inference to LLVM IR and assembly.

  2. Releasing the GIL and C callbacks · Expert · 13 min

    Use nogil=True to run compiled code in Python threads concurrently, and @cfunc to hand native callbacks to C libraries.

  3. Extending Numba with @overload · Expert · 14 min

    Teach Numba to compile your own functions or unsupported library calls with the high-level @overload API.