我想集成并行处理以使我的for循环运行得更快。Python:Joblib中的并行处理使代码运行速度更慢
但是,我注意到它只是让我的代码运行得更慢。请参阅下面的示例,其中我使用joblib
,并在随机整数列表上使用了简单函数。请注意,没有的并行处理运行速度快于和。
任何有关正在发生的事情的见解?
def f(x):
return x**x
if __name__ == '__main__':
s = [random.randint(0, 100) for _ in range(0, 10000)]
# without parallel processing
t0 = time.time()
out1 = [f(x) for x in s]
t1 = time.time()
print("without parallel processing: ", t1 - t0)
# with parallel processing
t0 = time.time()
out2 = Parallel(n_jobs=8, batch_size=len(s), backend="threading")(delayed(f)(x) for x in s)
t1 = time.time()
print("with parallel processing: ", t1 - t0)
我得到以下输出:
without parallel processing: 0.0070569515228271484
with parallel processing: 0.10714387893676758
并行处理,因为涉及到更复杂的安装程序的额外开销。您通常不希望并行完成需要微秒的任务。 – Muposat
我也试过它在一个更复杂的模糊匹配函数,它仍然花了很长时间 –
[为什么以下简单的并行化代码比Python中的简单循环慢得多?](https://stackoverflow.com/questions/46727090/why-the-following-simple-parallelized-code-slow-than-a-simple-loop-in) – user3666197