Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

multiprocessing with 5 processes slower than single process

I am trying to run 5 parallel processes and have also divided the list in 5 blocks but it takes more time than linearly processing in a single process, I have tried other solutions on Stackoverflow but the results are same. If I run the code, this is what I get:

result

import multiprocessing as mp
import time


def raiseNum(pwr, nums):
    for v in nums:
        x = v**pwr 


if __name__ == '__main__':

    # Let's get the cube of all numbers in the list nums
    nums = [i for i in range(0, 100000000, 1)]
    pwr = 3

    ## linear processing
    start = time.time()

    results_l = raiseNum(pwr, nums)

    end = time.time()

    print('Linear Processing time: ', str(end-start), 'Seconds')
    


    ## Parallel processing: 5 Processes
    
    # divide nums list in 5 parts
    blockSize = len(nums)//5
    
    numsBlocks = [nums[i: i+blockSize] for i in range(0, len(nums), blockSize)]

    processList = []

    p0 = mp.Process(target=raiseNum, args=(pwr, numsBlocks[0]))
    p1 = mp.Process(target=raiseNum, args=(pwr, numsBlocks[1]))
    p2 = mp.Process(target=raiseNum, args=(pwr, numsBlocks[2]))
    p3 = mp.Process(target=raiseNum, args=(pwr, numsBlocks[3]))
    p4 = mp.Process(target=raiseNum, args=(pwr, numsBlocks[4]))

    start = time.time()

    p0.start()
    p1.start()
    p2.start()
    p3.start()
    p4.start()

    p0.join()
    p1.join()
    p2.join()
    p3.join()
    p4.join()

    end = time.time()

    print('5 Parallel Processes time: ', str(end-start), 'Seconds')

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The optimization depends both on your hardware as well as the computation. When I ran your same code on my machine I got the following results:

Linear Processing time:  27.75643253326416 sec
5 Parallel Processes total time:  7.949779510498047 sec
Computation time:  1.7256593704223633 sec
Join time:  6.224120140075684 sec 

Two points to note:

  • The parallel execution is almost 3.5 times faster. (I tested your code on a machine with 24 cores)
  • When we split the parallel processing into two parts, we can notice that join takes considerably longer (as the computation is not that intensive).

Here is the code, where I added a couple of extra print statements towards the end:

import multiprocessing as mp
import time


def raiseNum(power, numbers):
    for v in numbers:
        x = v ** power


if __name__ == '__main__':
    # Let's get the cube of all numbers in the list nums
    nums = [i for i in range(0, 100000000, 1)]
    pwr = 3

    # linear processing
    start = time.time()
    raiseNum(pwr, nums)
    end = time.time()
    print('Linear Processing time: ', str(end - start), 'sec')

    # Parallel processing: 5 Processes
    # divide nums list in 5 parts
    blockSize = len(nums) // 5
    numsBlocks = [nums[i: i + blockSize] for i in range(0, len(nums), blockSize)]
    processList = []
    p0 = mp.Process(target=raiseNum, args=(pwr, numsBlocks[0]))
    p1 = mp.Process(target=raiseNum, args=(pwr, numsBlocks[1]))
    p2 = mp.Process(target=raiseNum, args=(pwr, numsBlocks[2]))
    p3 = mp.Process(target=raiseNum, args=(pwr, numsBlocks[3]))
    p4 = mp.Process(target=raiseNum, args=(pwr, numsBlocks[4]))

    start1 = time.time()

    p0.start()
    p1.start()
    p2.start()
    p3.start()
    p4.start()

    start2 = time.time()

    p0.join()
    p1.join()
    p2.join()
    p3.join()
    p4.join()

    end = time.time()

    print('5 Parallel Processes total time: ', str(end - start1), 'sec')
    print('Computation time: ', str(start2 - start1), 'sec')
    print('Join time: ', str(end - start2), 'sec')

Hope this helps with better optimization.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading