Skip to content

Numba lecture: @jit vs @njit usage and cache=True in Jupyter #516

@mmcky

Description

@mmcky

Summary

During review of the downstream translation sync PR (QuantEcon/lecture-python-programming.zh-cn#17), several issues were identified in the Numba lecture content introduced by PR #512. These are source content concerns, not translation bugs.

Issues

1. @jit vs @njit equivalence claim may mislead readers

The lecture note states that @jit and @njit are now equivalent since nopython mode is the default. However, in some environments @jit can still fallback to object mode without raising an error. This could lead to readers getting inconsistent results, especially when they expect compilation failures to surface as errors.

Suggestion: Consider using @njit explicitly in all examples, or add a caveat about environment-specific behavior.

2. @jit(parallel=True) should be @njit(parallel=True)

The parallelization example uses @jit(parallel=True) with prange. Numba's parallel optimization requires the nopython compilation path; if @jit falls back to object mode, parallel=True may silently not take effect. Using @njit(parallel=True) (or @jit(nopython=True, parallel=True)) would guarantee the example behaves as described.

3. cache=True limitation in Jupyter environments

The new "Caching Compiled Code" section explains that cache=True stores compiled code to disk for reuse. However, Numba's disk caching typically requires functions to be defined in locatable module files. In Jupyter / Jupyter Book interactive environments (which this lecture is built with), cache=True may not work or may raise errors. A brief note about this limitation would help readers avoid confusion.

Source

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions