nopython mode jit yields worse performance with unique function

0 views
Skip to first unread message

William Beard

unread,
Mar 29, 2017, 4:40:52 PM3/29/17
to Numba Public Discussion - Public
Here's a simplified version of `np.unique` that seems to be slowed by numba (in nopython mode)

    from numba import njit

   
def unique_np(ar):
        std
= np.sort(ar)
        flag
= np.concatenate((np.array([True]), std[1:] != std[:-1]))
       
return std[flag]

    unique_jit
= njit(unique_np)


   
# Check they both work
    inp
= np.array([6, 1, 2, 2, 3])
   
assert (unique_jit(inp) == unique_np(inp)).all()
   
assert (np.unique(inp) == unique_np(inp)).all()

    
For following input the original has a better time on my machine

 
    nr.seed(0)
    ri
= nr.randint(0, high=1000, size=10000)
   
assert (unique_jit(ri)  == np.unique(ri)).all()  # checking again...
   
   
%timeit unique_np(ri)   # => 1000 loops, best of 3: 427 µs per loop
   
%timeit unique_jit(ri)  # => 1000 loops, best of 3: 594 µs per loop

    
Am I doing something un-numban here? I'm curious because this is the first example of performance degradation I've seen.

Jason Sachs

unread,
Mar 29, 2017, 5:27:03 PM3/29/17
to numba...@continuum.io
it's not your code, it's the way that numba seems to handle np.sort:

def just_sort(x):
    return np.sort(x)
jitsort = njit(just_sort)

jitsort(ri)   # prime the jit

%timeit just_sort(ri)  # 1000 loops, best of 3: 409 µs per loop
%timeit jitsort(ri)    # 1000 loops, best of 3: 634 µs per loop

--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users+unsubscribe@continuum.io.
To post to this group, send email to numba...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/4bf4ffc5-2b78-4e0a-a186-fbceaef76f2c%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Jason Sachs

unread,
Mar 29, 2017, 5:28:09 PM3/29/17
to numba...@continuum.io
see also: 


Warning

Sorting may be slightly slower than Numpy’s implementation.

Reply all
Reply to author
Forward
0 new messages