为什么 python joblib 并行处理比单个 cpu 慢?

Why is python joblib parallel processing slower than single cpu?

我想了解为什么使用 joblib 的并行处理比单个 cpu 操作慢? 下面是我的代码。

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))

输出:

Time taken parallel: 2.4427331139999997
Time taken single cpu: 0.00196953699999991

产生进程的开销比计算时间要高得多。实际上,在此上下文中使用多处理没有任何好处。

如果您更改函数,您将开始观察到改进。

例如,让我们用简单的递归斐波那契函数改变当前函数。

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