Numba has the amazing ability to speed up loops with JIT compilation. The key twist, however, is that when using numpy, one isn't allowed to create any new arrays. Fortunately, most numpy functions include an optional out parameter for writing output to -- except numpy.sort. The most obvious alternative is numpy.ndarray.sort, which IS in place,
@njit("void(f8[:])")
def sort_inplace(arr):
arr.sort()
but this fails to compile,
...
...
...
/Users/duckworthd/anaconda/lib/python2.7/site-packages/numba/typeinfer.pyc in propagate(self)
293 print("propagate".center(80, '-'))
294 oldtoken = newtoken
--> 295 self.constrains.propagate(self.context, self.typevars)
296 newtoken = self.get_state_token()
297 if config.DEBUG:
/Users/duckworthd/anaconda/lib/python2.7/site-packages/numba/typeinfer.pyc in propagate(self, context, typevars)
112 raise
113 except Exception as e:
--> 114 raise TypingError("Internal error:\n%s" % e, constrain.loc)
115
116
TypingError: Internal error:
Attribute 'sort' of array(float64, 1d, A) is not typed
Short of re-implementing a sorting algorithm, is there a way to sort a numpy array in a JIT-compiled numba loop?
arr.sortis C code, not a Python loop. Andnp.sort(a)just makes a copy ofa, and does sort on it. So what's there to speedup? - hpauljkindparameter. - hpaulj