In my code, there are a lot of parameters which are constant during the running. I defined a dict
type variable to store them. But I find that numba
cannot support dict
.
What is a better way to solve this?
Numba does not fully support the Python dict because it is an untyped container that can have any Python types as members. To generate efficient machine code, Numba needs the keys and the values of the dictionary to have fixed types, declared in advance. To achieve this, Numba has a typed dictionary, numba.
Both Cython and Numba speeds up Python code even small number of operations. More the number of operations more is the speed up. However, performance gain by Cython saturates at around 100-150 times of Python. On the other hand, speed up gain by Numba increases steadily with number of operations.
In nopython mode, the Numba compiler will generate code that does not access the Python C API. This mode produces the highest performance code, but requires that the native types of all values in the function can be inferred.
Numba reads the Python bytecode for a decorated function and combines this with information about the types of the input arguments to the function. It analyzes and optimizes your code, and finally uses the LLVM compiler library to generate a machine code version of your function, tailored to your CPU capabilities.
Assuming you have a function like this and you are fine by accessing it as attribute instead of by subscript:
import numba as nb
@nb.njit
def func(config):
return config.c
You could use a collections.namedtuple
here (like @JoshAdel mentioned):
import numpy as np
from collections import namedtuple
conf = namedtuple('conf', ['a', 'b', 'c'])
func(conf(1, 2.0, np.array([1,2,3], dtype=np.int64)))
# array([1, 2, 3], dtype=int64)
Or a jitclass:
spec = [('a', nb.int64),
('b', nb.float64),
('c', nb.int64[:])]
@nb.jitclass(spec)
class Conf:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
func(Conf(1, 2.0, np.array([1,2,3], dtype=np.int64)))
# array([1, 2, 3], dtype=int64)
These can't replace all functionalities of a dictionary but these allow to pass in "a lot of parameters" as one instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With