Joblib simple example parallel example slower than simple

淺唱寂寞╮ 提交于 2019-12-25 16:54:06

问题


from math import sqrt
from joblib import Parallel, delayed
import time

if __name__ == '__main__':

    st= time.time()
    #[sqrt(i ** 2) for i in range(100000)]  #this part in non parellel
    Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(100000))
    print time.time()-st

now the non parelle part runs in 0.4 sec while parallel part runs for 18 sec .. I am confused why would this happen


回答1:


Parallel processes (which joblib creates) require copying data. Imagine it this way: you have two people who each carry a rock to their house, shine it, then bring it back. That's loads slower than one person shining them on-the-spot.

All the time is wasted in transit, rather than being spent on the actual calculation. You will only benefit from parallel processes for more substantial computational tasks.


If you care about speeding up this specific operation: Use numpy's vectorized math operations. On my machine, parallel: 1.13 s, serial: 54.6 ms, numpy: 3.74 ms.

    a = np.arange(100000, dtype=np.int)
    np.sqrt(a ** 2)

Don't worry about libraries like Cython or Numba; they won't speed up this already performant operation.



来源:https://stackoverflow.com/questions/44084513/joblib-simple-example-parallel-example-slower-than-simple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!