@njit, what is the purpose?

from numba import njit

This is from some code from Github. The author then used a lot of

@njit 

before definitions of functions.

What is the purpose of such codes? These codes can always be ignored in reading the codes, right?

This is a question about numba, a particular Python package. You can read a brief introduction here (along with lots of other documentation).

The short answer is that the njit decorator is telling numba to compile the decorated function the first time it is called. numba’s big claim to fame is that it is numpy-aware, meaning that it can compile code that uses numpy arrays directly. This can greatly speed up numerical code.

You can ignore the decorators when reading the code, but if you are thinking of modifying the code you should check to make sure that numba is still able to compile the result.

1 Like

Thanks a lot for the reply!

What troubles me a lot is the naming. Why ‘njit’? a strange name.

It’s a variation of the jit decorator, standing for “Just In Time” compilation, a very common term in this area.

The n derives from the central difference between jit and njit, the later had the option no_python defaulting to True instead of False, make sure that code changes don’t accidentally make numba change the compilation model to something significantly slower. Check the documentation for more details.

2 Likes