logo


numba


<p><a href="https://numba.pydata.org">Numba</a> is a tool that can make numeric code much faster in python. It offers a just in time compiler that can turn your functions into fast machine code and it can offer critical speedups. It also plays nice with <a href="https://numpy.org">numpy</a>.</p>


1 - Introduction
2 - Compile
3 - Benchmark
4 - Types
5 - Vectorize
6 - Conclusion

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.