I used Numba in my research to speed up Python programs. I stumbled upon various errors during implementation, so I will share that knowledge as a sample.
In my case, the calculation of the particle swarm optimization method using the Runge-Kutta method is 33 times faster, from about 2000 seconds to about 60 seconds.
※Caution※ --Refer to the references for the basic usage of Numba. --All use cases use the nopython mode `` `njit```. --Numba may work without specifying the argument or return type, but here it is assumed that you specify all.
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.15.2
BuildVersion: 19C57
$ python -V
Python 3.8.5
$ pip freeze
numba==0.51.0
numpy==1.19.1
Numba can be installed with the following command.
$ pip install numba
np.empty
in the functionWhen using numba, using `` `np.empty``` in a function may result in an error. In that case, specifying the type as follows worked fine.
main.py
import numpy as np
from numba import njit
@njit("f8[:,:]()")
def func():
x = np.empty((1, 2), dtype=np.float64)
return x
print(func())
To return multiple return values, write as `Tuple ((i8, i8))`
.
Note that the parentheses are doubled.
main.py
import numpy as np
from numba import njit
@njit("Tuple((i8, i8))(i8, i8)")
def func(x, y):
return x, y
print(func(1, 2))
When dealing with multidimensional lists in Numba, write as `f8 [:,:]`
.
Since it is two-dimensional, it does not mean that there are two colons, but it seems that two colons are sufficient for any number of dimensions.
main.py
import numpy as np
from numba import njit
@njit("f8[:,:](f8[:,:])")
def func(x):
return x ** 2
x = np.random.rand(5, 5)
print(func(x))
There are various ways to speed up Python, such as Cython and Julia, but I think the Numba method, which just writes decorators, is the easiest.
Although there are restrictions such as not being able to use classes and generators, I felt that it would be relatively easy to implement if the method was to speed up the bottleneck locally.
-Speeding up for statement by numba and argument of jit -Python acceleration Numba introduction 2
Recommended Posts