Skip to content

Accelerate Python Functions with Numba

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

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)

Refernce

  • https://numba.pydata.org/

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback: dig +short txt issue.imzye.com
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Buy Me a Coffee