简体   繁体   中英

Why does numba's parallel=True make this computation 3 times slower?

When doing this:

import numpy as np
from numba import jit

@jit
def doit(A, Q, n):
    for i in range(len(Q)):
        Q[i] = np.sum(A[i:i+n] <= A[i+n])

A = np.random.random(1000*1000)
n = 5000
Q = np.zeros(len(A)-n)    
doit(A, Q, n)

the runtime takes ~ 5.4 seconds on my computer.

I tried to use numba's parallelization feature:

@jit(parallel=True)
def doit(A, Q, n):
    for i in range(len(Q)):
        Q[i] = np.sum(A[i:i+n] <= A[i+n])

and instead, it takes 17 seconds.

Why does numba's parallel=True make this computation 3 times slower instead of faster?

I just found the answer: one character was missing: p range instead of range:

from numba import jit, prange

@jit(parallel=True)
def doit(A, Q, n):
    for i in prange(len(Q)):
       ...

Then it takes 1.8 seconds instead of 5.4 seconds: the parallelization worked.

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