Numba is running a lot slower than the raw Python

Viewed 43

I'm new to using Numba and keen to try and understand how it works.

I built a binary search function:

def binarysearch(alist, item):
        first = 0
        last = len(alist) - 1
        found = False

        while first<=last:
            midpoint = (first + last)//2            
            if alist[midpoint] == item:
                return midpoint
            else:
                if item < alist[midpoint]:
                    last = midpoint-1
                else:
                    first = midpoint+1  
        return -1

And if I run this as follows:

l = list(range(10000000))

%timeit
binarysearch(l,5000)

I get: 4.88 µs ± 315 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

If I do the same but use @numba.njit, I get: 19.8 s ± 533 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Any ideas for how to get the improvements of Numba?

versions: Python: 3.6.3 Numba: 0.40.0

0 Answers
Related