divRem by `-' performance

Serge D. Mechveliani mechvel at botik.ru
Wed Oct 17 17:02:37 CEST 2012


People,
consider the following contrived program for division with remainder:

----------------------------------------------------------------
qRem :: Int -> Int -> (Int, Int)
qRem m n = if m < 0 || n <= 0 then  error "\nwrong arguments in qRem.\n"
           else                     qRem' 0 m
           where
           qRem' q r = if r < n then (q, r)  else qRem' (succ q) (r - n)

main = putStr $ shows (qRem n 5) "\n"
       where
       e = 25
       n = 2^e
----------------------------------------------------------------

Compilation in  ghc-7.4.1 :  

                ghc --make -O -rtsopts Main
Rinning:        time ./Main  +RTS -K.. -M.. -RTS

For  e = 25,  it takes the minimum of  -K80m -M280m
(on my Linux Debian machine).

Is not this memory eagerness strange?

Regards,
Sergei



More information about the Glasgow-haskell-users mailing list