Below is a Python implementation of the quicksort algorithm using parallelism. It takes about 1 second per every 10 items in the list, which is hilariously unacceptable. Why is it so slow?
from multiprocessing import *
def quicksort(lyst, connection=None):
if len(lyst) > 1:
pivot = lyst.pop(len(lyst)-1)
wall = 0
for i in range(len(lyst)):
if lyst[i] <= pivot:
lyst[wall], lyst[i] = lyst[i], lyst[wall]
wall += 1
receiveLeft, sendLeft = Pipe()
receiveRight, sendRight = Pipe()
Process(target=quicksort, args=(lyst[:wall], sendLeft)).start()
Process(target=quicksort, args=(lyst[wall:], sendRight)).start()
lyst = receiveLeft.recv() + [pivot] + receiveRight.recv()
if connection:
connection.send(lyst)
connection.close()
return lyst
if __name__ == '__main__':
quicksort([8,4,6,9,1,3,10,2,7,5])
EDIT Thanks for the responses. As it turns out, switching to Threads and limiting the number of them I was spawning sped things up. However, my linear version of the algorithm still performed better.