Reputation: 369
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:
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')
Upvotes: 0
Views: 77
Reputation: 1299
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:
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.
Upvotes: 2