Numba : Accelerates Python codes
Methods to accelerate python codes
To make python numeric computations faster we have many alternative approaches such as Cython, TensorFlow, PyTorch, Chainer, Pythran, and PyPy. But for this we need to convert python to different languages.
What is the easiest way to achieve good performance in python without in-depth knowledge of libraries ?
Numba is an open-source JIT compiler (A Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting byte-code every time a method is invoked, will compile the byte-code into the machine code instructions of the running machine, and then invoke this object code instead.) that translates python functions to optimised machine code at runtime using the industry standard LLVM compiler library. Numba complied 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 Nimba decorators to your Python function, and Numba does the rest.
About Numba
Numba is designed to be used with Numpy arrays and functions. Numba generates specialised code for different arrays data types and layouts to optimize performance. Special decorators can create universal functions that broadcast over Numpy arrays just like Numpy functions do.
Numba also works with Jupyter notebooks for interactive computing, and with distributive execution frameworks.
The below code is implemented using numba which looks similar to python with jit (just-in-time) decorator. Lets see the difference in execution time.
It takes a little more time to execute for the first time. But in second execution it takes minimal time.
Numba offers a range of options for parallelising your code for CPUs and GPUs, often with only minor code changes.
Numba strives to support as much of the Python language as possible, but some language features are not available inside Numba-compiled functions.Below is a quick reference for the support level of Python constructs.
In this blog, I have discussed about numba a useful library that you should know to make your python code faster.
Hope this blog will help you learn a new library.