问题
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 ms, total: 440 ms
Wall time: 442 ms
This is very fast, but doesn't compile with Numba:
Invalid use of Function(<built-in function mul>) with argument(s) of type(s): (UniTuple(Literal[int](0) x 1), int64)
This is much slower:
def createTuple():
for _ in range(100000):
tuplex = tuple(0 for _ in range(1000))
%time createTuple()
CPU times: user 5.28 s, sys: 3.28 ms, total: 5.29 s
Wall time: 5.29 s
And also fails to compile:
The use of yield in a closure is unsupported.
I am very new to Python and Numba. Is there a way to get a tuple of length N (known at compile time) create - hopefully efficiently - with Numba?
回答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:
@njit
def foo(tup):
...
foo(tuple(np.zeros(100)))
Unfortunately, tuple(np.zeros(100))
is relatively slow.
来源:https://stackoverflow.com/questions/62852267/how-to-efficiently-create-a-tuple-of-length-n-with-code-that-will-compile-with-n