Skip to content

Accelerate Python Function with Numba

programming-banner

What is Numba?

Numba is an open-source library that allows you to optimize Python code by compiling it to machine code. This means that the code can be executed much faster than if it were interpreted. Numba works by just-in-time (JIT) compiling Python functions to native machine code, which makes them much faster.

Numba translates Python functions to optimized machine code at runtime using the industry-standard LLVM compiler library. Numba-compiled numerical algorithms in Python can approach the speeds of C or FORTRAN.

You don’t need to replace the Python interpreter, run a separate compilation step, or even have a C/C++ compiler installed. Just apply one of the Numba decorators to your Python function, and Numba does the rest.

Example

One of the great things about Numba is that it is quite easy to use. To use Numba, you just need to add a few decorators to your functions. These decorators tell Numba which functions to compile, and how to compile them. For example, here is how you would use Numba to optimize a Python function that calculates the sum of squares of a list of numbers:

from numba import jit
import numpy as np
import time

x = np.random.rand(10000000)

@jit
def sum_sq(a):
    result = 0
    N = len(a)

    for i in range(N):
        result+= a[i]
    return result
s_t = (time.time())
sum_sq.py_func(x)
s_t_1 = time.time()
sum_sq(x)
s_t_2 = time.time()
print(s_t_1 - s_t, s_t_2 - s_t_1)

How Numba Accelerates Python Functions

Numba works by analyzing the code of your Python function, and then generating machine code that can execute that code much faster. Numba uses a technique called “type specialization” to generate machine code that is optimized for the specific data types that are used in your function. This means that Numba can produce machine code that is much faster than what you would get by just using the Python interpreter.

Another way that Numba accelerates Python functions is by using parallelization. Numba can automatically parallelize some loops in your code, which can greatly increase performance. This is particularly useful when working with large datasets, as it allows you to take advantage of the full power of your CPU.

Conclusion

Python is a powerful language, but its performance can be a limiting factor when working with large datasets. Numba is a great tool that can help you optimize your Python functions and make them much faster. By using Numba, you can take full advantage of your CPU and get the most out of your Python code. Whether you are a data scientist, machine learning engineer, or just someone who wants to speed up their Python code, Numba is definitely worth checking out.

Refernce

  • https://numba.pydata.org/
Leave a message