I’m writing a parallel qsort algorithm, but he working slow than common implementation.
I think the problem is in function ‘concat’. How to speed up the algorithm?
(defn qsort-orig [L]
(if (empty? L)
'()
(let [[pivot & l] L]
(concat (qsort-orig (for [y l :when (< y pivot)] y))
(list pivot)
(qsort-orig (for [y l :when (>= y pivot)] y))))))
(defn qsort [L]
(if (empty? L)
'()
(let [ [pivot & l] L
Left (for [y l :when (< y pivot)] y)
Right (for [y l :when (>= y pivot)] y)]
(concat (apply concat (pmap qsort
(if (list? Left)
Left
(list Left))))
(list pivot)
(apply concat (pmap qsort
(if (list? Right)
Right
(list Right))))))))
# for test
(time (qsort (repeatedly 10000 #(rand-int 10000))))
(time (qsort-orig (repeatedly 10000 #(rand-int 10000))))
It is likely that the memory allocation times for both of these are washing out the real time differences between them.
recurinqsort-origthen it wont blow the stack as quickly and should run a lot faster because it will spend less time allocating memory.