Sunday, March 25, 2018

Numba makes python greatly faster

Numba is a module developed by Anaconda and that makes python greatly faster using Just In Time Compiler (JIT compiler).

How to install

You can install Numba with pip.
$ pip3 install numba

I installed it this way:
$ python3.5 -m pip install --user numba

How to use

You can use numba with a "jit" decorator.
from numba import jit

@jit
def example():
    for ....blabla
    return something

You can specify the type of returned value (A) and the type of the arguments (B1, B2, B3, ...).
from numba import jit

@jit(A(B1,B2,...))
def example(B1, B2, ...):
    for ....blabla
    return something

Examples

Examples of Numba.
from numba import jit

@jit
def sum_jit(x):
    sum_num = 0
    for i in range(x):
        sum_num += i
    return sum_num

@jit('int64(int64)')
def sum_jit_type_specified(x):
    sum_num = 0
    for i in range(x):
        sum_num += i
    return sum_num


print(sum_jit(10000000))
print(sum_jit_type_specified(10000000))

It was incredibly fast!!
$ python3.5 test.py
49999995000000
49999995000000


Want to make .exe with Python

Maybe use Cython.
Maybe optimization like C/C++ is also possible for Cython? (like -Ofast, O2, O3 option..)