0
votes

I'm trying to understand why the parallel processing using joblib is slower than single cpu operation? Below is my code.

from joblib import Parallel, delayed
import multiprocessing
import time

inputs = range(10000)
def processInput(i):
    return i * i

if __name__ == '__main__':

    num_cores = multiprocessing.cpu_count()
    start_time = time.process_time()
    results = Parallel(n_jobs=num_cores)(delayed(processInput)(i) for i in inputs)
    #print(results)
    print(str(time.process_time() - start_time))

    results=[]
    start_time = time.process_time()
    for i in inputs:
        results.append(processInput(i))
    #print(results)
    print(str(time.process_time() - start_time))

Output:

Time taken parallel: 2.4427331139999997
Time taken single cpu: 0.00196953699999991
1

1 Answers

4
votes

The overhead introduced to spawn the processes is much higher than the computation time. In practice there is no gain to use multiprocessing in this context.

If you change your function you will start observing improvements.

For instance, let's just change the current function with a naive recursive Fibonacci function.

inputs = range(25, 35)

def processInput(n):
    if n < 2:
        return n
    return processInput(n-2) + processInput(n-1)


Time taken parallel: 0.06956500000000002
Time taken single cpu: 8.295273