Skip to content

Tools \ Numba

Numba
Tool's logo

Numba is an open-source JIT compiler that translates a subset of Python and NumPy code into fast machine code.

Source code

Maturity : Maintained | Categories : Python Stack, Code Optimization and Distribution | License : | Producer : The Numba Team


Overview

Numba is an open-source Just-In-Time (JIT) compiler that translates a subset of Python and NumPy code into fast machine code using the LLVM compiler infrastructure. It is primarily designed to speed up numerical computing by optimizing the execution of loops and functions that rely heavily on arrays and matrix operations.

Numba integrates well with Python scientific libraries like NumPy and can speed up execution without requiring major changes to your code.

Quick Start Guide

import numpy as np
import time
from numba import jit

# Using Numba to compile the function for better performance
@jit(nopython=True)
def sum_of_squares_numba(arr):
    total = 0
    for i in arr:
        total += i ** 2
    return total

# Create an array with one million random values
arr = np.random.rand(10**6)

start = time.time()
result = sum_of_squares_numba(arr)
end = time.time()

print(f"Result: {result}")
print(f"Time with Numba: {end - start} seconds")
  • Without Numba: The function sum_of_squares computes the sum of squares of a NumPy array, but iterating over the array with a standard Python loop can be slow for large datasets.
  • With Numba: By adding the @jit(nopython=True) decorator, Numba compiles the function into optimized machine code, significantly improving performance for large arrays.

Installation

To install Numba, you can use the following command in your terminal:

pip install numba

Resources

Tutorials