简体   繁体   中英

why is numba so much faster on this simple summation?

I have a public notebook where python, numpy, numba, cython, and fortran are compared on simple summation:

https://gist.github.com/denfromufa/7727874c4fe1e7e174ed953930e93bbc

Why is numba so much faster?

As pointed out by @DavidW, you aren't really doing a comparison of identical algorithms. Below I've written two separate functions for each cython and numba that do the same thing. The first operates on an array, the second is just given an integer:

Cython:

cpdef long cy_sum(long[:] A):
    cdef long i, n = A.shape[0], s = 0
    for i in range(n):
        s += A[i]
    return s

cpdef long cy_sum2(long i):
    cdef long s, x
    s = 0
    for x in range(i):
        s += x
    return s

Numba:

@nb.jit(nopython=True)
def nb_sum(A):
    s=0
    n = A.shape[0]
    for i in range(n):
        s += A[i]
    return s

@nb.jit(nopython=True)
def nb_sum2(i):
    s=0
    for x in range(i):
        s+=x
    return s

Testing to make sure they give the same results:

N = int(1e6)
d = np.arange(N, dtype=np.int64)

print np.allclose(nb_sum(d), cy_sum(d))   # True
print np.allclose(nb_sum2(N), cy_sum2(N))  # True

And timings on my hardware:

%timeit cy_sum(d)
%timeit nb_sum(d)

1000 loops, best of 3: 416 µs per loop
1000 loops, best of 3: 237 µs per loop

%timeit cy_sum2(N)
%timeit nb_sum2(N)

10000000 loops, best of 3: 63.5 ns per loop
10000000 loops, best of 3: 187 ns per loop

I wouldn't draw too many conclusions from a micro-benchmark like this, but at least now equivalent implementations are being compared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM