Python, using multiprocess is slower than not using it

前端 未结 4 1040
情话喂你
情话喂你 2020-12-29 13:04

After spending a lot of time trying to wrap my head around multiprocessing I came up with this code which is a benchmark test:

Example 1:

         


        
4条回答
  •  清酒与你
    2020-12-29 13:24

    This thread has been very useful!

    Just a quick observation over the good second code provided by David Robinson above (answered Jan 8 '12 at 5:34), which was the code more suitable to my current needs.

    In my case I had previous records of the running times of a target function without multiprocessing. When using his code to implement a multiprocessing function his timefunc(multi) didn't reflect the actual time of multi, and it rather appeared to reflect the time expended in the parent.

    What i did was to externalise the timing function and the time that I got looked more like expected:

     start = timefunc()
     multi()/single()
     elapsed = (timefunc()-start)/(--number of workers--)
     print(elapsed)
    

    In my case with a double core the total time carried out by 'x' workers using the target function was twice faster than running a simple for-loop over the target function with 'x' iterations.

    I am new to multiprocessing so please be cautious with this observation though.

提交回复
热议问题