Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on the Python module numba

Installation

numba can be installed using the following command.

:::bash
pip3 install numba

If you need CUDA support, you have to install CUDA drivers.

:::bash
sudo apt-get install cuda-10-1

Instead of going through the hassle of configuring numba for GPU, a better way is to run numba in a Nvidia Docker container. The Docker image nvidia/cuda:10.1-cudnn7-runtime-ubuntu18.04 has CUDA runtime installed, so it is as easy as installing numba on top it and you are ready to go. For more detailed instructions, please refer to .

In [1]:
from __future__ import division
import time
import random

data = []
data_length = 100000
ma_length = 500
test_times = 10

for i in range(data_length):
    data.append(random.randint(1, 100))
In [2]:
import numba


@numba.jit
def ma_numba(data, ma_length):
    ma = []
    data_window = data[:ma_length]
    test_data = data[ma_length:]

    for new_tick in test_data:
        data_window.pop(0)
        data_window.append(new_tick)
        sum_tick = 0
        for tick in data_window:
            sum_tick += tick
        ma.append(sum_tick / ma_length)

    return ma


start = time.time()

for i in range(test_times):
    result = ma_numba(data, ma_length)

time_per_test = (time.time() - start) / test_times
time_per_point = time_per_test / (data_length - ma_length)

print("time_per_test:%sS" % time_per_test)
print("time_per_point:%sMS" % (time_per_point * 1000000))
/workspace/.pip-modules/lib/python3.7/site-packages/numba/ir_utils.py:1969: NumbaPendingDeprecationWarning: 
Encountered the use of a type that is scheduled for deprecation: type 'reflected list' found for argument 'data' of function 'ma_numba'.

For more information visit http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-reflection-for-list-and-set-types

File "<ipython-input-2-72e6018139c1>", line 4:
@numba.jit
def ma_numba(data, ma_length):
^

  warnings.warn(NumbaPendingDeprecationWarning(msg, loc=loc))
time_per_test:0.4571639060974121S
time_per_point:4.594612121582031MS

Deprecation Notices

Reflection for List and Set types will be deprecated in Numba. You should use numba.typed.List and numba.typed.Set instead.

In [ ]:
 

Comments