python - Speeding up take with numba? -
is possible speed np.take using numba?
here attempt, lot slower. can't use nopython mode not np.empty_like command.
import numba import numpy np timer import timer def take( x, indices ): result = np.empty_like( indices, dtype=x.dtype ) in range( len( indices ) ): result[i] = x[ indices[ ] ] return result jtake = numba.jit("f4[:](f4[:],i4[:])" )( take ) if __name__=='__main__': n = 100000 m = 100 idx = np.random.random_integers( 0, n, m ) x = np.random.randn( n ) num_tests=10000 timer( 'take' ): in range( num_tests ): r0 = take( x, idx ) timer( 'numba take' ): in range( num_tests ): r1 = jtake( x, idx ) timer( 'numpy.take' ): in range( num_tests ): r2 = x.take( idx )
which has results:
beginning take take took 2.46 seconds beginning numba take numba take took 1.11 seconds beginning numpy.take numpy.take took 0.04 seconds
the answer no.
numba not act on compiled functions such np.take()
or array methods work fancy indexing, basis algorithm. numba acts on interpreted part of code.
your take()
function has more overhead numpy's , numba has improved loops (interpreted).
with numpy > 1.9 code should closer numpy's take since algorithm based on fancy indexing , they improved fancy indexing efficiency level of np.take()
.
Comments
Post a Comment