Todavía estoy aprendiendo Haskell y escribí la siguiente función de clasificación de radix. Parece que funciona correctamente, pero el problema es que es bastante ineficiente en la memoria. Si se compila con ghc, la memoria supera ampliamente los 500MB con una lista de entrada de elementos de tamaño 10000.Optimización del orden de radix en Haskell
Así que quiero preguntarle cómo podría mejorarse el siguiente algoritmo/código para hacerlo más eficiente en términos de velocidad y memoria. ¿Cuál es el mejor lugar para comenzar?
import System.Random
-- radixsort for positive integers. uses 10 buckets
radixsort :: [Int] -> [Int]
radixsort [] = []
radixsort xs =
-- given the data, get the number of passes that are required for sorting
-- the largest integer
let maxPos = floor ((log (fromIntegral (foldl max 0 xs))/log 10) + 1)
-- start sorting from digit on position 0 (lowest position) to position 'maxPos'
radixsort' ys pos
| pos < 0 = ys
| otherwise = let sortedYs = radixsort' ys (pos - 1)
newBuckets = radixsort'' sortedYs [[] | i <- [1..10]] pos
in [element | bucket <- newBuckets, element <- bucket]
-- given empty buckets, digit position and list, sort the values into
-- buckets
radixsort'' [] buckets _ = buckets
radixsort'' (y:ys) buckets pos =
let digit = div (mod y (10^(pos + 1))) (10^pos)
(bucketsBegin, bucketsEnd) = splitAt digit buckets
bucket = head bucketsEnd
newBucket = bucket ++ [y]
in radixsort'' ys (bucketsBegin ++ [newBucket] ++ (tail bucketsEnd)) pos
in radixsort' xs maxPos
-- get an random array given an seed
getRandIntArray :: Int -> [Int]
getRandIntArray seed = (randomRs (0, div (maxBound :: Int) 2) (mkStdGen seed))
main = do
value <- (\x -> return x) (length (radixsort (take 10000 (getRandIntArray 0))))
print value
¿Ha considerado utilizar matrices de la mónada IO? – Gabe
Gracias, definitivamente revisaré otros tipos de datos tan pronto como me sienta más cómodo con los conceptos básicos de Haskell. – Timo
En 'maxPos' deberías usar' foldl'' en lugar de 'foldl'. Además, ¿no está 'floor (x + 1)' mejor expresado como 'techo x'? –