Why is the following simple parallelized code much slower than a simple loop in Python?

后端 未结 2 1019
小鲜肉
小鲜肉 2020-12-19 09:24

A simple program which calculates square of numbers and stores the results:

    import time
    from joblib import Parallel, delayed
    import multiprocessi         


        
2条回答
  •  -上瘾入骨i
    2020-12-19 09:56

    From the documentation of threading:

    If you know that the function you are calling is based on a compiled extension that releases the Python Global Interpreter Lock (GIL) during most of its computation ...

    The problem is that in the this case, you don't know that. Python itself will only allow one thread to run at once (the python interpreter locks the GIL every time it executes a python operation).

    threading is only going to be useful if myfun() spends most of its time in a compiled Python extension, and that extension releases the GIL.

    The Parallel code is so embarrassingly slow because you are doing a huge amount of work to create multiple threads - and then you only execute one thread at a time anyway.

    If you use the multiprocessing backend, then you have to copy the input data into each of four or eight processes (one per core), do the processing in each processes, and then copy the output data back. The copying is going to be slow, but if the processing is a little bit more complex than just calculating a square, it might be worth it. Measure and see.

提交回复
热议问题