我正在编写一个并行 qsort 算法,但他的工作速度比普通实现慢。我认为问题出在函数“concat”中。如何加快算法速度?
(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))))