logo

... numba: introduction



Notes

You can install numba via pip.

python -m pip install numba

Once installed you should be able to repeat the listed experiment.

def func_one(n):
    result = 0
    for i in range(n):
        squared = n * n 
        result += squared
    return result

def func_two(n):
    result = 0
    squared = n * n 
    for i in range(n):
        result += squared
    return result

You can test the speed of both functions.

%timeit func_one(10000)
%timeit func_two(10000)

You can now try again after using the decorator.

import numba as nb

@nb.njit
def func_one(n):
    result = 0
    for i in range(n):
        squared = n * n 
        result += squared
    return result

@nb.njit
def func_two(n):
    result = 0
    squared = n * n 
    for i in range(n):
        result += squared
    return result

func_one(1); func_two(2);

If you now time both functions you'll notice they after faster and equally fast.

Feedback? See an issue? Something unclear? Feel free to mention it here.



If you want to be kept up to date, consider signing up for the newsletter.