How To Efficiently Create A Tuple Of Length N With Code That Will Compile With Numba?
I timed two ways to create a tuple of length N. This is very fast: def createTuple(): for _ in range(100000): tuplex = (0,) * 1000 CPU times: user 439 ms, sys: 1.01 m
Solution 1:
From the numba (0.50) documentation:
Note
The
tuple()
constructor itself is NOT supported.
So, in numba code, tuples need to be either supplied as function arguments or initialized as literals, like (0, 1, 2, 3)
. This is unfortunate, because it means that operations involving array shapes require tuples in numba, even though they work fine with int arrays in regular numpy:
shape = np.arange(1, 4)
np.zeros(shape) # OK for normal python code, not in an @njit function.
You'll have to refactor your code to have tuple creation outside the @njit function:
@njitdeffoo(tup):
...
foo(tuple(np.zeros(100)))
Unfortunately, tuple(np.zeros(100))
is relatively slow.
Post a Comment for "How To Efficiently Create A Tuple Of Length N With Code That Will Compile With Numba?"