skyindeer
skyindeer

Reputation: 175

Why numba parallel=True is slower than parallel=False

I am trying to perform parallel computing using numba, however, I encountered a problem: using the following code, it is significantly slower if I switch parallel=True on compare to switching it off.

import numpy as np
from numba import njit, prange
from numba.typed import List
import time


n = 1000

@njit(parallel = True)
def lamb(now):
    timez = List()
    k1 = List()
    k = 0
    for i in range(n):
        timez.append(np.arange(i+1))
        k1.append( len(timez[i][timez[i]<=now]) )

    for i in prange(n):
       k += k1[i]

    return k


lamb(21)

start = time.time()
lamb(21)
end = time.time()
print("Elapsed (after compilation) = %s" % (end - start))

If parallel = True, the elapsed time is Elapsed (after compilation) = 0.012674093246459961. In comparasion, if parallel = False, the elapsed time is Elapsed (after compilation) = 0.007932901382446289.

Any idea why this is the case?

Upvotes: 2

Views: 2484

Answers (1)

William Bright
William Bright

Reputation: 535

Coordination between different processes adds a cost to performance. That is the performance penalty you see, if the work was truly worth the effort to parallelize then you would reap net benefits assuming the work was large enough.

Upvotes: 2

Related Questions