Example of Numba ‘for loop’ execution with “empty” numpy array population

This example is perfect for showing how fast Numba can be with when put right with Nopython mode (@jit(nopython=True) or@njit)

import numpy as np
import numba as nb
from numba import jit, njit

import time
start_time = time.time()

# @nb.jit(nb.float64[:](nb.float64[:]))
def f(arr):
    res = np.zeros(len(arr))

    for i in range(len(arr)):
        res[i] = (arr[i]) ** 2

    return res

arr = np.random.rand(10000000)

print("--- %s seconds ---" % (time.time() - start_time))

Original (without @njit) takes 7.3
While Numba takes only 0.58s

That’s more than 7 times faster!