February 2019
Intermediate to advanced
672 pages
16h 50m
English
Getting started with Numba is fairly straightforward. As a first example, we will implement a function that calculates the sum of squares of an array. The function definition is as follows:
def sum_sq(a): result = 0 N = len(a)
for i in range(N): result += a[i] return result
To set up this function with Numba, it is sufficient to apply the nb.jit decorator:
from numba import nb @nb.jit def sum_sq(a): ...
The nb.jit decorator won't do much when applied. However, when the function will be invoked for the first time, Numba will detect the type of the input argument, a , and compile a specialized, performant version of the original function.
To measure the performance gain obtained by the Numba compiler, we can compare ...