Here is my current code. It's a naive implementation.
import System.Environment (getArgs)
minmax [] = Nothing
minmax [x] = Just (x, x)
minmax (a:b:xs) = Just $ minmax' xs $ sort a b
where minmax' [] lohi = lohi
minmax' [x] lohi@(lo, hi)
| x < lo = (x, hi)
| x > hi = (lo, x)
| otherwise = lohi
minmax' (x:y:xs) (lo, hi) = minmax' xs $ newlo `seq` newhi `seq` (newlo, newhi)
where (lo', hi') = sort x y
newlo = if lo' < lo then lo' else lo
newhi = if hi' > hi then hi' else hi
sort x y = if x < y then (x, y) else (y, x)
main = do
args <- getArgs
let [from, till] = map read args :: [Int]
print $ minmax [from..till]
I want to see how far I can go with haskell compared to c++ rust
and maybe using three approaches in all languages.
Naive, naive parallel and using matrices (repa
for Haskell and eigen
for C++)
From this profiler report I see that Haskell allocates a lot of memory.
minmax +RTS -p -RTS 1 10000000
total time = 0.13 secs (132 ticks @ 1000 us, 1 processor)
total alloc = 800,062,584 bytes (excludes profiling overheads)
I've compiled with ghc -O2 -fllvm
(ghc 8.2.1). From my point of view naive implementation shouldn't allocate much memory because it should just iterate a list in pairs. How to achieve that, is there anything else I can do to improve performance?
I wanted to try stream-fusion
package, but it doesn't support base 4.10
. Is there workaround for this? Couldn't find anything about this problem.